【深入浅出IOS开发】截取屏幕到文件中

在这里首先明确,View中显示的东西都在在layer中的。

我们通过renderInContext来渲染layer中的内容

[objc]  view plain copy
  1. - (void)viewDidLoad {  
  2.     [super viewDidLoad];  
  3.     //截取当前的View  
  4.     //1.创建图层  
  5.     UIGraphicsBeginImageContextWithOptions(self.view.frame.sizeNO0.0);  
  6.     //2.渲染layer  
  7.     CGContextRef ctr = UIGraphicsGetCurrentContext();  
  8.     [self.view.layer renderInContext:ctr];  
  9.     CGContextStrokePath(ctr);  
  10.     //3.取出image  
  11.     UIImage *imageNew = UIGraphicsGetImageFromCurrentImageContext();  
  12.     //4.关闭图层  
  13.     UIGraphicsEndImageContext();  
  14.       
  15.     //5.将图片转换成NSData  
  16.     NSData *data = UIImagePNGRepresentation(imageNew);  
  17.     [data writeToFile:@"/Users/misaka/Desktop/1.png" atomically:YES];  
  18.     // Do any additional setup after loading the view, typically from a nib.  
  19. }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值