#pragma mark - 点击
- (IBAction)save
{
PHAuthorizationStatus oldStatus = [PHPhotoLibrary authorizationStatus];
// 判断当前的授权状态
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
switch (status) {
// 这是系统级别的限制(比如家长控制),用户也无法修改这个授权状态
case PHAuthorizationStatusRestricted: {
[SVProgressHUD showErrorWithStatus:@"由于系统原因,无法保存图片!"];
break;
}
// 用户已经拒绝当前App访问相片数据(说明用户当初选择了“Don't Allow”)
case PHAuthorizationStatusDenied: {
if (oldStatus != PHAuthorizationStatusNotDetermined) {
XMGLog(@"提醒用户去打开访问开关")
}
break;
}
// 用户已经允许当前App访问相片数据(说明用户当初选择了“OK”)
case PHAuthorizationStatusAuthorized: {
[self saveImage];
break;
}
default:
break;
}
}];
}
/**
* 获得当前App对应的【自定义相册】
*/
- (PHAssetCollection *)createdCollection
{
// 抓取所有【自定义相册】
PHFetchResult<PHAssetCollection *> *collections = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
// 从Info.plist中获得App名称(也就是当前App的相册名称)
NSString *title = [NSBundle mainBundle].infoDictionary[(NSString *)kCFBundleNameKey];
for (PHAssetCollection *collection in collections) {
if ([collection.localizedTitle isEqualToString:title]) {
// 【自定义相册】已经创建过
return collection;
}
}
__block NSString *collectionId = nil;
[[PHPhotoLibrary sharedPhotoLibrary] performChangesAndWait:^{
// 创建【自定义相册】
collectionId = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:title].placeholderForCreatedAssetCollection.localIdentifier;
} error:nil];
// 根据id获得刚刚创建完的相册
return [PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers:@[collectionId] options:nil].firstObject;
}
/**
* 保存图片
*/
- (void)saveImage
{
// 凡是遵守NSFastEnumeration协议的对象,都可以使用for-in来遍历, for-in的效率普通for循环高
// performChanges里面不能嵌套performChanges
// 获得【自定义相册】
PHAssetCollection *createdCollection = self.createdCollection;
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
// 保存图片到【相机胶卷】
// createdAsset 就代表 刚才添加到【相机胶卷】中的图片
PHObjectPlaceholder *createdAsset = [PHAssetChangeRequest creationRequestForAssetFromImage:self.imageView.image].placeholderForCreatedAsset;
// 将对应的相册传入,创建一个【相册修改请求】对象
PHAssetCollectionChangeRequest *collectionChangeRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:createdCollection];
// 将保存到【相机胶卷】的图片添加到【自定义相册】
[collectionChangeRequest insertAssets:@[createdAsset] atIndexes:[NSIndexSet indexSetWithIndex:0]];
} completionHandler:^(BOOL success, NSError *error) {
if (success) {
[SVProgressHUD showSuccessWithStatus:@"保存成功!"];
} else {
[SVProgressHUD showErrorWithStatus:@"保存失败!"];
}
}];
// __block NSString *assetId = nil;
// // 凡是遵守NSFastEnumeration协议的对象,都可以使用for-in来遍历, for-in的效率普通for循环高
// [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
// // 保存图片到【相机胶卷】
// // createdAsset 就代表 刚才添加到【相机胶卷】中的图片
// assetId = [PHAssetChangeRequest creationRequestForAssetFromImage:self.imageView.image].placeholderForCreatedAsset.localIdentifier;
// } completionHandler:^(BOOL success, NSError *error) {
// PHAsset *createdAsset = [PHAsset fetchAssetsWithLocalIdentifiers:@[assetId] options:nil].firstObject;
//
// // 将对应的相册传入,创建一个【相册修改请求】对象
// PHAssetCollectionChangeRequest *collectionChangeRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:createdCollection];
//
// // 将保存到【相机胶卷】的图片添加到【自定义相册】
// [collectionChangeRequest insertAssets:@[createdAsset] atIndexes:[NSIndexSet indexSetWithIndex:0]];
// }];
}
- (IBAction)back
{
[self dismissViewControllerAnimated:YES completion:nil];
}
网友评论