iOS APP删除系统相册中选中的图片-姬云鹏
Posted tarena_3G
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS APP删除系统相册中选中的图片-姬云鹏相关的知识,希望对你有一定的参考价值。
app有一个新功能是在app中删除相册的照片。 8之前还是AssetsLibary处理
iOS 8+通过Photos.framework #import <Photos/Photos.h>
1.添加图片
- //首先获取相册的集合
- PHFetchResult *collectonResuts = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAny options:[PHFetchOptions new]] ;
- //对获取到集合进行遍历
- [collectonResuts enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop)
- PHAssetCollection *assetCollection = obj;
- //Camera Roll是我们写入照片的相册
- if ([assetCollection.localizedTitle isEqualToString:@"Camera Roll"])
- [[phphotoLibrary sharedPhotoLibrary] performChanges:^
- //请求创建一个Asset
- PHAssetChangeRequest *assetRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:[UIImage imageNamed:@"pet"]];
- //请求编辑相册
- PHAssetCollectionChangeRequest *collectonRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:assetCollection];
- //为Asset创建一个占位符,放到相册编辑请求中
- PHObjectPlaceholder *placeHolder = [assetRequest placeholderForCreatedAsset ];
- //相册中添加照片
- [collectonRequest addAssets:@[placeHolder]];
- completionHandler:^(BOOL success, NSError *error)
- NSLog(@"Error:%@", error);
- ];
- ];
-
2.删除最后一张图片
- PHFetchResult *collectonResuts = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAny options:[PHFetchOptions new]] ;
- [collectonResuts enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop)
- PHAssetCollection *assetCollection = obj;
- if ([assetCollection.localizedTitle isEqualToString:@"Camera Roll"])
- PHFetchResult *assetResult = [PHAsset fetchAssetsInAssetCollection:assetCollection options:[PHFetchOptions new]];
- [assetResult enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop)
- [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^
- //获取相册的最后一张照片
- if (idx == [assetResult count] - 1)
- [PHAssetChangeRequest deleteAssets:@[obj]];
- completionHandler:^(BOOL success, NSError *error)
- NSLog(@"Error: %@", error);
- ];
- ];
- ];
3.删除选中的图片
- // NSURL *url= [info objectForKey:UIImagePickerControllerReferenceURL];
- PHFetchResult *result = [PHAsset fetchAssetsWithALAssetURLs:@[url] options:nil];
- PHAsset *asset = [result lastObject];
- [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^
- [PHAssetChangeRequest deleteAssets:@[asset]];
- completionHandler:^(BOOL success, NSError *error)
- NSLog(@"Error: %@", error);
- ];
以上是关于iOS APP删除系统相册中选中的图片-姬云鹏的主要内容,如果未能解决你的问题,请参考以下文章