iOS之图片保存到相册
不多说先看效果好不好
savecd.gif
下载网络图片的SDWebImage 还有加载图片的DACircularProgress这两个第三方库
所以在控制器要到导入这两个
import <UIImageView+WebCache.h>
import <DALabeledCircularProgressView.h>
这里是加载图片主要代码
[self.picture sd_setImageWithURL:[NSURL URLWithString:@"http://wimg.spriteapp.cn/ugc/2016/11/09/5822a9157534a.gif"] placeholderImage:nil options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) { // receivedSize : 已经接收的图片大小 // expectedSize : 图片的总大小 // LYWLog(@"已经接收的图片大小:%ld� ========== 图片的总大小 %ld",receivedSize,expectedSize); CGFloat progress = 1.0 * receivedSize / expectedSize; self.progressView.trackTintColor = [UIColor grayColor]; self.progressView.progress = progress; self.progressView.hidden = NO; self.progressView.progressLabel.text = [NSString stringWithFormat:@"%.0f%%", progress * 100]; } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) { self.progressView.hidden = YES; }];
保存到相册的主要代码
这里使用到 <Photos/Photos.h>这个系统的这个库
/* PHAuthorizationStatusNotDetermined, 用户还没有做出选择 PHAuthorizationStatusDenied, 用户拒绝当前应用访问相册(用户当初点击了"不允许") PHAuthorizationStatusAuthorized 用户允许当前应用访问相册(用户当初点击了"好") PHAuthorizationStatusRestricted, 因为家长控制, 导致应用无法方法相册(跟用户的选择没有关系) */ // 判断授权状态 PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus]; if (status == PHAuthorizationStatusRestricted) { // 因为家长控制, 导致应用无法方法相册(跟用户的选择没有关系) [SVProgressHUD showErrorWithStatus:@"因为系统原因, 无法访问相册"]; } else if (status == PHAuthorizationStatusDenied) { // 用户拒绝当前应用访问相册(用户当初点击了"不允许") NSLog(@"提醒用户去[设置-隐私-照片-xxx]打开访问开关"); } else if (status == PHAuthorizationStatusAuthorized) { // 用户允许当前应用访问相册(用户当初点击了"好") [self saveImage]; } else if (status == PHAuthorizationStatusNotDetermined) { // 用户还没有做出选择 // 弹框请求用户授权 [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) { if (status == PHAuthorizationStatusAuthorized) { // 用户点击了好 [self saveImage]; } }]; } } - (void)saveImage { // PHAsset : 一个资源, 比如一张图片\一段视频 // PHAssetCollection : 一个相簿 // PHAsset的标识, 利用这个标识可以找到对应的PHAsset对象(图片对象) __block NSString *assetLocalIdentifier = nil; // 如果想对"相册"进行修改(增删改), 那么修改代码必须放在[PHPhotoLibrary sharedPhotoLibrary]的performChanges方法的block中 [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{ // 1.保存图片A到"相机胶卷"中 // 创建图片的请求 assetLocalIdentifier = [PHAssetCreationRequest creationRequestForAssetFromImage:self.picture.image].placeholderForCreatedAsset.localIdentifier; } completionHandler:^(BOOL success, NSError * _Nullable error) { if (success == NO) { [self showError:@"保存图片失败!"]; return; } // 2.获得相簿 PHAssetCollection *createdAssetCollection = [self createdAssetCollection]; if (createdAssetCollection == nil) { [self showError:@"创建相簿失败!"]; return; } [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{ // 3.添加"相机胶卷"中的图片A到"相簿"D中 // 获得图片 PHAsset *asset = [PHAsset fetchAssetsWithLocalIdentifiers:@[assetLocalIdentifier] options:nil].lastObject; // 添加图片到相簿中的请求 PHAssetCollectionChangeRequest *request = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:createdAssetCollection]; // 添加图片到相簿 [request addAssets:@[asset]]; } completionHandler:^(BOOL success, NSError * _Nullable error) { if (success == NO) { [self showError:@"保存图片失败!"];; } else { [self showSuccess:@"保存图片成功!"];; } }]; }]; } /** * 获得相簿 */ - (PHAssetCollection *)createdAssetCollection { // 从已存在相簿中查找这个应用对应的相簿 PHFetchResult<PHAssetCollection *> *assetCollections = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil]; for (PHAssetCollection *assetCollection in assetCollections) { if ([assetCollection.localizedTitle isEqualToString:LYWAssetCollectionName]) { return assetCollection; } } // 没有找到对应的相簿, 得创建新的相簿 // 错误信息 NSError *error = nil; // PHAssetCollection的标识, 利用这个标识可以找到对应的PHAssetCollection对象(相簿对象) __block NSString *assetCollectionLocalIdentifier = nil; [[PHPhotoLibrary sharedPhotoLibrary] performChangesAndWait:^{ // 创建相簿的请求 assetCollectionLocalIdentifier = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:LYWAssetCollectionName].placeholderForCreatedAssetCollection.localIdentifier; } error:&error]; // 如果有错误信息 if (error) return nil; // 获得刚才创建的相簿 return [PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers:@[assetCollectionLocalIdentifier] options:nil].lastObject; } - (void)showSuccess:(NSString *)text { dispatch_async(dispatch_get_main_queue(), ^{ [SVProgressHUD showSuccessWithStatus:text]; }); } - (void)showError:(NSString *)text { dispatch_async(dispatch_get_main_queue(), ^{ [SVProgressHUD showErrorWithStatus:text]; }); }