十 、Quartz 2D 截屏
版本1,将图片保存到电脑桌面
- (IBAction)btnClick:(UIButton *)sender { UIGraphicsBeginImageContextWithOptions(self.view.frame.size, NO, 0); CGContextRef ctx = UIGraphicsGetCurrentContext(); [self.view.layer renderInContext:ctx]; UIImage *img = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); NSData *data = UIImagePNGRepresentation(img); [data writeToFile:@"/Users/你电脑的名字/Desktop/aa.png" atomically:YES]; }
版本2,将图片保存到相册
- (IBAction)btnClick:(UIButton *)sender { UIGraphicsBeginImageContextWithOptions(self.view.frame.size, NO, 0); CGContextRef ctx = UIGraphicsGetCurrentContext(); // 将屏幕绘制到上下文中 [self.view.layer renderInContext:ctx]; UIImage *img = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); // 将图片保存到相册 UIImageWriteToSavedPhotosAlbum(img, self, @selector(image:didFinishSavingWithError:contextInfo:), nil); } - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{ if (error) { NSLog(@"保存失败"); }else{ NSLog(@"保存成功"); } }