标签:
分为全屏截图、区域截图
1 //截图 2 //1.创建画板 3 UIGraphicsBeginImageContext([self getWindow].bounds.size); 4 5 //2、将layer画在画板上 6 [self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; 7 8 //从当前画板上得到图片 9 UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 10 11 //销毁画板 12 CGImageRef imageRef = image.CGImage; 13 14 //设置区域范围,再在图片上进行区域截图 15 CGRect imageRect = CGRectMake(0, 0, 1, 1); 16 17 CGImageRef newimage = CGImageCreateWithImageInRect(imageRef, imageRect); 18 19 self.imageView.image = [UIImage imageWithCGImage:newimage]; 20 21 NSData *imageData = UIImagePNGRepresentation(image); 22 23 //保存图片比较卡,应在异步分线程完成 24 UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil); 25 26 [self saveFileWith:imageData fileName:@"1" Extent:@"jpg"];
1 //保存图片 2 -(void)saveFileWith:(NSData *)data fileName:(NSString *)name Extent:(NSString *)Extent 3 { 4 NSString *documentDirectory = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject; 5 6 NSString *path = [documentDirectory stringByAppendingString:[NSString stringWithFormat:@"%@.%@",name,Extent]]; 7 8 if ([data writeToFile:path atomically:YES]) { 9 NSLog(@"保存图片成功path =%@",documentDirectory); 10 } 11 }
标签:
原文地址:http://www.cnblogs.com/niaoniao/p/5422909.html