利用当前界面的一些元素,生成一张图片,保存到系统相册
首先要有想要让其成为图片的底图,UIView、UILabel、UIButton都行
toImage=[[UIView alloc]initWithFrame:CGRectMake(100, 150, 160, 130)]; [self.view addSubview:toImage]; toImage.backgroundColor=[UIColor greenColor];
在一个点击事件里生成图片
-(void)iamgebtn:(UIButton*)btn{ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ // toImage:要生成为图片的view UIGraphicsBeginImageContextWithOptions(toImage.bounds.size, 0, [[UIScreen mainScreen] scale]); [toImage.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); } }!!!!注意,
因为涉及到访问相册,所以先在plist文件里面添加NSPhotoLibraryUsageDescription允许应用程序访问你的相册,不然看不到结果呦。。。
demo地址:
https://github.com/SmileKe/ToImage
欢迎下载
转载于:https://www.cnblogs.com/keke0820/p/7216045.html
