首先配置工程写入权限键值对:
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:@"保存图片成功"];
}
}