View生成图片

 

利用当前界面的一些元素,生成一张图片,保存到系统相册

首先要有想要让其成为图片的底图,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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值