美文网首页
iOS如何把一个view生成一张图片,并且保存到本地

iOS如何把一个view生成一张图片,并且保存到本地

作者: Persistence__ | 来源:发表于2017-06-23 11:45 被阅读0次

因为涉及到访问相册,所以先在plist文件里面添加NSPhotoLibraryUsageDescription允许应用程序访问你的相册

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

// self.testView:要生成为图片的view

UIGraphicsBeginImageContextWithOptions(self.testView.bounds.size, 0, [[UIScreen mainScreen] scale]);

[self.testView.layer renderInContext:UIGraphicsGetCurrentContext()];

UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

UIImageWriteToSavedPhotosAlbum(viewImage, self, @selector(imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:), nil);

});

}

- (void)imageSavedToPhotosAlbum:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo

{

if (!error) {

NSLog(@"成功");

}else {

NSLog(@"失败 - %@",error);

}

}

相关文章

网友评论

      本文标题:iOS如何把一个view生成一张图片,并且保存到本地

      本文链接:https://www.haomeiwen.com/subject/flcccxtx.html