十 、Quartz 2D 截屏

时间:2016-04-11 20:23:27   收藏:0   阅读:220

 

 

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

 

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

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!