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

 

posted @ 2016-04-11 19:17  人生路1/5  阅读(155)  评论(0编辑  收藏  举报