iOS APP删除系统相册中选中的图片
http://blog.csdn.net/wsk_123_123/article/details/51445418
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); }];
相关推荐
liuxudong00 2020-11-19
wwzaqw 2020-11-11
lihaoxiang 2020-11-05
CrossingX 2020-11-04
xuegangic 2020-10-17
fanxiaoxuan 2020-09-17
安卓猴 2020-09-12
惠秀宝 2020-09-08
wwzaqw 2020-09-04
chenyingSunny 2020-09-04
是nsacer先森的 2020-09-03
bigname 2020-08-25
gaoqiang00 2020-08-24
灵均兰草 2020-08-20
xjp 2020-08-17
webgm 2020-08-16
wiky 2020-08-05
歆萌 2020-08-03
89961330 2020-07-28