标签:
步骤:
1. 开启位图上下文
2. 获取上下文
3. 把控件上的图层渲染到上下文,layer只能用渲染不能绘制,不能用drawAt...方法
4. 获取生成的图片
5. 关闭上下文
6. image转data,首先得知道生成一种什么样格式的图片,png,jpg。
7. 写入到指定位置(如桌面)
代码:
- (void)viewDidLoad {
    [super viewDidLoad];
    //
    self.imageView.image = [UIImage imageNamed:@"高圆圆"];
    
    //1.开启位图上下文
    UIGraphicsBeginImageContextWithOptions(self.imageView.frame.size, NO, 0);
    
    //2.获取上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    
    //3.把控件上的图层渲染到上下文,layer只能用渲染不能绘制,不能用drawAt...方法
    [self.imageView.layer renderInContext:ctx];
    
    //4.获取生成的图片
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    
    //5.关闭上下文
    UIGraphicsEndImageContext();
    
    //6.image转data,首先得知道生成一种什么样格式的图片,png,jpg。
    NSData *data = UIImagePNGRepresentation(image);
    
    //7.写入到指定位置(如桌面)
    [data writeToFile:@"/Users/swift/Desktop/高圆圆.png" atomically:YES];
}
截取图片写入到桌面:
标签:
原文地址:http://blog.csdn.net/bao_libra/article/details/51360489