http://blog.csdn.net/wsk_123_123/article/details/51445418
app有一个新功能是在app中删除相册的照片。 8之前还是AssetsLibary处理
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); }];