我正在制作一个绘图应用程序,用户将图形保存为在照片中以编程方式创建的自定义相册中的图像。我想删除这些保存的图像,请告诉我如何从自定义相册中删除图像。
感谢。
答案 0 :(得分:1)
它会起作用,
导入断言库#import <AssetsLibrary/AssetsLibrary.h>
首先,您应检索并存储任何数据库的图像路径
您可以使用以下代码检索存储的图像路径
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library writeImageToSavedPhotosAlbum:[[UIImage imageNamed:@"Dhoni.jpg"] CGImage] orientation:(ALAssetOrientation)[[UIImage imageNamed:@"Dhoni.jpg"] imageOrientation] completionBlock:^(NSURL *assetURL, NSError *error){
if (error) {
NSLog(@"error");
} else {
NSLog(@"url %@", assetURL);
}
}];
之后,通过使用路径,您可以删除图像
NSURL *deleteurl = [NSURL URLWithString: assetURL];
NSArray *arrDelete = [[NSArray alloc] initWithObjects:deleteurl , nil];
PHFetchResult *asset = [PHAsset fetchAssetsWithALAssetURLs:arrDelete options:nil];
[asset enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSLog(@"%@",[obj class]);
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
BOOL req = [obj canPerformEditOperation:PHAssetEditOperationDelete];
if (req) {
NSLog(@"true");
[PHAssetChangeRequest deleteAssets:@[obj]];
}
} completionHandler:^(BOOL success, NSError *error) {
NSLog(@"Finished Delete asset. %@", (success ? @"Success." : error));
if (success) {
NSLog(@"delete successfully");
}else{
NSLog(@"delete Cancel");
}
}];
}];
答案 1 :(得分:0)
删除自定义相册
IOS 10及以上:
注意:用户将获得弹出确认。
__block PHFetchResult *photosAsset;
__block PHAssetCollection *collection;
__block PHObjectPlaceholder *placeholder;
// Find the album
PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init];
NSString * collectionTitle = //.. the custom album name
fetchOptions.predicate = [NSPredicate predicateWithFormat:@"title = %@", collectionTitle];
// this is how we get a match for album Title held by 'collectionTitle'
NSArray * collections = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAny options:fetchOptions];
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
[collections enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSLog(@"Item %li = %@", (unsigned long)idx, obj);
[PHAssetCollectionChangeRequest deleteAssetCollections:@[obj]];
}];
} completionHandler:^(BOOL success, NSError *error) {
if (success)
{
NSLog(@"success");
NSLog(@"placeholder holds %@", placeholder.debugDescription );
}
else
{
NSLog(@"%@", error);
}
}];