码迷,mamicode.com
首页 > 其他好文 > 详细

十 、Quartz 2D 截屏

时间:2016-04-11 20:24:26      阅读:122      评论:0      收藏:0      [点我收藏+]

标签:

 

 

版本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(@"保存成功");
    }
    
}

 

十 、Quartz 2D 截屏

标签:

原文地址:http://www.cnblogs.com/kinghx/p/5379688.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!