iOS 学习笔记一【屏幕截图,并显示当前View】
// 直接上代码:
//
// ViewController.h
// 屏幕截图测试
//
// Created by 博爱之家 on 15/11/11.
// Copyright © 2015年 博爱之家. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
{
NSData *imageData;
}
@end
*************************************
//
// ViewController.m
// 屏幕截图测试
//
// Created by 博爱之家 on 15/11/11.
// Copyright © 2015年 博爱之家. All rights reserved.
//
#import "ViewController.h"
//宏定义
//当前设备的屏幕宽度
#define KSCREEN_WIDTH [[UIScreen mainScreen] bounds].size.width
//当前设备的屏幕高度
#define KSCREEN_HEIGHT [[UIScreen mainScreen] bounds].size.height
@interface ViewController ()
@property (nonatomic, strong) UILabel *testlabel;
@property (nonatomic, strong) UIButton *testButton;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.title = @"截屏测试";
self.view.backgroundColor = [UIColor whiteColor];
self.testlabel = [UILabel new];
self.testlabel.frame = CGRectMake((KSCREEN_WIDTH-)/ , , , );
self.testlabel.text = @"截屏测试";
self.testButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.testButton.frame = CGRectMake((KSCREEN_WIDTH-)/ , , , );
[self.testButton setTitle:@"点击截屏" forState:UIControlStateNormal];
self.testButton.backgroundColor = [UIColor purpleColor];
[self.testButton addTarget:self action:@selector(clickBUutton:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.testlabel];
[self.view addSubview:self.testButton];
}
- (IBAction)clickBUutton:(id)sender
{
NSLog(@"开始");
[self saveScreenShotsView];
UIImageView *imageView = [[UIImageView alloc] init];
imageView.frame = CGRectMake(, , , );
imageView.backgroundColor = [UIColor greenColor];
imageView.image = [UIImage imageWithData:imageData];
[self.view addSubview:imageView];
}
// 保存图片
- (void)saveScreenShotsView
{
UIImage *image = [self getNormalImage:self.view];
UIImageWriteToSavedPhotosAlbum(image, self, nil, nil);
[self saveToDisk:image];
NSLog(@"结束");
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
#pragma mark - 获取屏幕截图
- (UIImage *)getNormalImage:(UIView *)view
{
UIGraphicsBeginImageContext(CGSizeMake(KSCREEN_WIDTH, KSCREEN_HEIGHT));
CGContextRef context = UIGraphicsGetCurrentContext();
[view.layer renderInContext:context];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
#pragma mark - 保存到硬盘中
- (void)saveToDisk:(UIImage *)image
{
NSString *dirPath = [NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES) objectAtIndex:];
NSLog(@"保存路径: %@", dirPath);
NSString *path = [NSString stringWithFormat:@"%@/pic_%f.png",dirPath,[NSDate timeIntervalSinceReferenceDate]];
imageData = [NSData dataWithData:UIImagePNGRepresentation(image)];
[imageData writeToFile:path atomically:YES];
NSLog(@"保存路径: %@", path);
NSString *imagePath = [[path componentsSeparatedByString:@"/"] lastObject];
NSLog(@"保存路径2imagePath: %@", imagePath);
NSLog(@"保存完毕");
}
@end
iOS 学习笔记一【屏幕截图,并显示当前View】的更多相关文章
- iOS学习笔记(5)——显示简单的TableView
1. 创建工程 创建一个新的Xcode工程命名为SimpleTableTest. 删除main.storyboard文件和info.plist中有关storyboard的相关属性. 按command+ ...
- iOS学习笔记(4)——显示单组件选取器
1. 创建工程 创建新工程,create a new Xcode project 创建single view application 创建名为PickerViewTest的工程 2. 创建xib文件 ...
- iOS学习笔记-精华整理
iOS学习笔记总结整理 一.内存管理情况 1- autorelease,当用户的代码在持续运行时,自动释放池是不会被销毁的,这段时间内用户可以安全地使用自动释放的对象.当用户的代码运行告一段 落,开始 ...
- iOS学习笔记10-UIView动画
上次学习了iOS学习笔记09-核心动画CoreAnimation,这次继续学习动画,上次使用的CoreAnimation很多人感觉使用起来很繁琐,有没有更加方便的动画效果实现呢?答案是有的,那就是UI ...
- iOS学习笔记总结整理
来源:http://mobile.51cto.com/iphone-386851_all.htm 学习IOS开发这对于一个初学者来说,是一件非常挠头的事情.其实学习IOS开发无外乎平时的积累与总结.下 ...
- IOS学习笔记(四)之UITextField和UITextView控件学习
IOS学习笔记(四)之UITextField和UITextView控件学习(博客地址:http://blog.csdn.net/developer_jiangqq) Author:hmjiangqq ...
- [置顶] iOS学习笔记47——图片异步加载之EGOImageLoading
上次在<iOS学习笔记46——图片异步加载之SDWebImage>中介绍过一个开源的图片异步加载库,今天来介绍另外一个功能类似的EGOImageLoading,看名字知道,之前的一篇学习笔 ...
- iOS学习笔记之UITableViewController&UITableView
iOS学习笔记之UITableViewController&UITableView 写在前面 上个月末到现在一直都在忙实验室的事情,与导师讨论之后,发现目前在实验室完成的工作还不足以写成毕业论 ...
- IOS学习笔记48--一些常见的IOS知识点+面试题
IOS学习笔记48--一些常见的IOS知识点+面试题 1.堆和栈什么区别? 答:管理方式:对于栈来讲,是由编译器自动管理,无需我们手工控制:对于堆来说,释放工作由程序员控制,容易产生memor ...
- iOS学习笔记-自己动手写RESideMenu
代码地址如下:http://www.demodashi.com/demo/11683.html 很多app都实现了类似RESideMenu的效果,RESideMenu是Github上面一个stars数 ...
随机推荐
- 使用jQuery操作DOM(ppt练习)
<!DOCTYPE html> <html> <head> <title>test3.html</title> <meta http- ...
- 还原数据库完整sq语句l
use master go declare @dbname varchar ( 20) set @dbname = 'QADB' declare @sql nvarchar ( 500) declar ...
- Cobbler安装CentOS 7网卡命名修改
准备上线CentOS 7.x,在Cobbler上导入后,发现网卡名称变成了eno1这样的,好吧,那就添加两个内核参数上去,让它变回eth0. cobbler profile edit --name=C ...
- Actionscript 3 自定义 matedata
metadata就是元数据 反应一个类本质的属性 可以通过describeType(obj)来得到反应该对象的xml 要自定义元数据,如[MyMatedata()] package { public ...
- solr6.6 solrJ索引富文本(word/pdf)文件
1.文件配置 在core下面新建lib文件夹,存放相关的jar包,如图所示: 修改solrconfig.xml <lib dir="${solr.install.dir:../../. ...
- Android手掌抑制功能的实现
近期须要实现一个功能,在Activity中有一个手写区域,为了更好的用户体验,须要满足即使整个手掌放在屏幕上时(android平板,屏幕比較大)也仅仅响应手写区域内的操作,即在支持多点触控的情况下,仅 ...
- go 中goroutine 的使用
一.多线程定义: 所谓的多线程,multithreading.有多线程能力的计算机因有硬件支持而能够在同一时间执行多于一个线程,进而提升整体处理性能.具有这种能力的系统包括对称多处理机.多核心处理器以 ...
- spock+maven+junitReport接口测试框架
1.POM 文件: <?xml version="1.0" encoding="UTF-8"?><project xmlns="ht ...
- html5学习整理-0311
整理一下今天所学的一些标签内容. 首先说一下DNS:全称Domain Name System,域名系统.是因特网上作为域名和IP地址相互映射的一个分布式数据库. URL协议:规定URL地址的格式,UR ...
- spring 动态定时任务
功能介绍:商品自动上架.按修改或添加时设置的自动上架时间而启动定时任务 更改商品状态为上架. spring 中配置文件 <?xml version="1.0" encodin ...