这里总结两种保存图片到相册的方法。
1.UIImageWriteToSavedPhotosAlbum()保存到相册,该方法图片会被压缩。
2.使用Photos框架中的PHPhotoLibrary来存储图片
1.UIImageWriteToSavedPhotosAlbum()存储图片
- (void)saveImages:(NSArray *)imageArrs {
for (int i = 0; i <imageArrs.count; i++) {
[self.downloadImageArrs addObject:imageArrs[i]];
}
PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
// 2.判断用户的授权状态
if (status == PHAuthorizationStatusNotDetermined) {
// 如果状态是不确定的话,block中的内容会等到授权完成再调用
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
// 授权完成就会调用
if (status == PHAuthorizationStatusAuthorized) {
//调用存储图片的方法
[self CWWSavePhotos];
}
}];
//如果允许访问
} else if (status == PHAuthorizationStatusAuthorized) {
//调用存储图片的方法
[self CWWSavePhotos];
//如果权限是拒绝
} else {
//弹出一个页面提示用户去打开授权
[MBProgressHUD showInWindowMessage:@"进入设置打开允许访问相册开关"];
}
//储图片的方法
- (void)CWWSavePhotos {
if (self.downloadImageArrs.count > 0) {
[self saveimage:[self.downloadImageArrs objectAtIndex:0]];
}
}
#pragma mark - 保存图片的方法
- (void)saveimage:(NSString *)imageUrl {
NSURL *imgUrl = [NSURL URLWithString:imageUrl];
[[SDWebImageDownloader sharedDownloader] downloadImageWithURL:imgUrl completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
if (image) {
UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
//清除sd缓存,防止图片被sdwebimage缓存
[[SDImageCache sharedImageCache] clearDiskOnCompletion:nil];
[[SDImageCache sharedImageCache] clearMemory];
}
}];
}
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
if (error) {
[MBProgressHUD showInWindowMessage:@">_< 保存失败"];
} else {
[self.downloadImageArrs removeObjectAtIndex:0];
if (self.downloadImageArrs.count==0) {
[MBProgressHUD showInWindowMessage:@"^_^ 保存成功"];
}
}
[self CWWSavePhotos];
}
2.使用Photos框架中的PHPhotoLibrary来存储图片
- (void)saveImages:(NSArray *)imageArrs {
for (int i = 0; i <imageArrs.count; i++) {
[self.downloadImageArrs addObject:imageArrs[i]];
}
PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
// 2.判断用户的授权状态
if (status == PHAuthorizationStatusNotDetermined) {
// 如果状态是不确定的话,block中的内容会等到授权完成再调用
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
// 授权完成就会调用
if (status == PHAuthorizationStatusAuthorized) {
//调用存储图片的方法
[self CWWSavePhotos];
}
}];
//如果允许访问
} else if (status == PHAuthorizationStatusAuthorized) {
//调用存储图片的方法
[self CWWSavePhotos];
//如果权限是拒绝
} else {
//弹出一个页面提示用户去打开授权
[MBProgressHUD showInWindowMessage:@"进入设置打开允许访问相册开关"];
}
}
//储图片的方法
- (void)CWWSavePhotos {
if (self.downloadImageArrs.count > 0) {
[self saveToPhoto:[self.downloadImageArrs objectAtIndex:0]];
}
}
#pragma mark - 获取在图库中是否已经创建该App的相册
//该方法的作用,获取系统中所有的相册,进行遍历,若是已有相册,返回该相册,若是没有返回nil,参数为需要创建 的相册名称
- (PHAssetCollection *)fetchAssetColletion:(NSString *)albumTitle {
// 获取所有的相册
PHFetchResult *result = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
//遍历相册数组,是否已创建该相册
for (PHAssetCollection *assetCollection in result) {
if ([assetCollection.localizedTitle isEqualToString:albumTitle]) {
return assetCollection;
}
}
return nil;
}
#pragma mark - 保存图片的方法
- (void)saveToPhoto:(NSString *)imageUrl{
NSURL *imgUrl = [NSURL URLWithString:imageUrl];
[[SDWebImageDownloader sharedDownloader] downloadImageWithURL:imgUrl completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
NSLog(@"dataLen:%lu",data.length);
if (finished)
{
UIImage * imgsave = image;
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
// 调用判断是否已有该名称相册
NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
//以项目名字命名相册
NSString *app_Name = [infoDictionary objectForKey:@"CFBundleDisplayName"];
PHAssetCollection *assetCollection = [self fetchAssetColletion:app_Name];
//创建一个操作图库的对象
PHAssetCollectionChangeRequest *assetCollectionChangeRequest;
if (assetCollection) {
// 已有相册
assetCollectionChangeRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:assetCollection];
} else {
// 1.创建自定义相册
assetCollectionChangeRequest = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:app_Name];
}
// 2.保存你需要保存的图片到系统相册
PHAssetChangeRequest *assetChangeRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:imgsave];
// 3.把创建好图片添加到自己相册(ps:想保存到系统相册,就注释掉下边一行代码就ok)
//这里使用了占位图片,为什么使用占位图片呢
//这个block是异步执行的,使用占位图片先为图片分配一个内存,等到有图片的时候,再对内存进行赋值
PHObjectPlaceholder *placeholder = [assetChangeRequest placeholderForCreatedAsset];
[assetCollectionChangeRequest addAssets:@[placeholder]];
} completionHandler:^(BOOL success, NSError * _Nullable error) {
if (error) {
// NSLog(@"保存失败");
} else {
//NSLog(@"保存成功");
[self.downloadImageArrs removeObjectAtIndex:0];
}
[self CWWSavePhotos];
}];
}
}];
}