Kanika Mishra
Kanika Mishra

Reputation: 23

How can i delete images from custom albums in iOS programmatically?

i am making a drawing app in which user saves the drawing as image in custom album created programmatically in photos. i want to delete these saved images, please tell me how can i delete images from custom album.

Thanks.

Upvotes: 1

Views: 1381

Answers (2)

Ofir Malachi
Ofir Malachi

Reputation: 1286

Delete custom Albums

IOS 10 and above:

NOTE: the user will get popup confirmation.

__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);
    }
    
}];

Upvotes: 0

Sathish Kumar VG
Sathish Kumar VG

Reputation: 2172

It will work,

Import Assert Library #import <AssetsLibrary/AssetsLibrary.h>

First you should retrieve and store image path to any database

you can use this below code for retrieve stored image path

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);
    }  
}];

After, by using path you can delete image

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");

        }
    }];

}];

Upvotes: 1

Related Questions