iOS图片写入到相册

首先配置工程写入权限键值对:
Privacy - Photo Library Additions Usage Description
例如:需要您的同意才能访问您的相册以保存图片
代码:

//参数1:图片对象
//参数2:成功方法绑定的target
//参数3:成功后调用方法
//参数4:需要传递信息(成功后调用方法的参数)
UIImageWriteToSavedPhotosAlbum(self.imageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
#pragma mark -- <保存到相册>
-(void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
    NSString *msg = nil ;
    if(error){
        msg = @"保存图片失败" ;
    }else{
        msg = @"保存图片成功" ;
    }
}

   

参考逻辑:

- (void)clickLoad
{
    if (self.tempImage) {
        UIImageWriteToSavedPhotosAlbum(self.tempImage, self, @selector(image: didFinishSavingWithError: contextInfo:), nil);
    }else{
        __weak ViewController *weakSelf = self;
        SDWebImageManager *manager = [SDWebImageManager sharedManager];
        [manager downloadImageWithURL:[NSURL URLWithString:self.str] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
            
        } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
            if (error) {
                NSLog(@"下载图片失败");
            }else{
                weakSelf.tempImage = image;
                UIImageWriteToSavedPhotosAlbum(self.tempImage, self, @selector(image: didFinishSavingWithError: contextInfo:), nil);
            }
            
        }];
    }
    
}
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
    if (error) {
        DebugLog(@"保存图片失败");
        [[Singleton sharesongle] setTipMessage:@"如需下载图片请设置访问权限"];
    }else{
        [[Singleton sharesongle] setTipMessage:@"保存图片成功"];
    }
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容