ALAssetsLibrary delete ALAssetsGroup / ALAsset
Asked Answered
T

6

6

I have created "photos album" from my App, using IOS AssetsLibrary.

Reading ALAssetsLibrary,ALAssetsGroup and ALAsset documentations, i have seen methods to "addAsset","addAssetsGroupAlbumWithName".

Is there a way to delete PROGRAMMATICALLY my ALAssetsGroup and ALAsset. (the property 'editable' suppose to be TRUE because i create this data).

Tref answered 15/6, 2012 at 16:37 Comment(1)
You were given the correct answer. Why not accept it?Katharina
L
10

This is not possible using any documented API. Only the photos app can delete Albums. If you want this feature to be added to iOS, I would fill a feature request at https://feedbackassistant.apple.com/.

Linkous answered 15/6, 2012 at 22:28 Comment(0)
C
14

You can only delete the ALAsset which is created by your app with document API [ALAsset setImageData:metadata:completionBlock:] (But I have not found any API to delete a ALAssetGroup).

1). Add an image "photo.jpg" to your project 2). Save an image to asset library:

ALAssetsLibrary *lib = [ALAssetsLibrary new];
UIImage *image = [UIImage imageNamed:@"photo.jpg"];
[lib writeImageToSavedPhotosAlbum:image.CGImage metadata:@{} completionBlock:^(NSURL *assetURL, NSError *error) {
    NSLog(@"Write image %@ to asset library. (Error %@)", assetURL, error);
}];

3). Go to default gallery, you will find photo.jpg in your "Saved Photos" album.

4). Delete this image from asset library:

ALAssetsLibrary *lib = [ALAssetsLibrary new];
[lib enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
    [group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop) {
        if(asset.isEditable) {
            [asset setImageData:nil metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) {
                NSLog(@"Asset url %@ should be deleted. (Error %@)", assetURL, error);
            }];
        }
    }];
} failureBlock:^(NSError *error) {

}];

5). Go to default gallery, you will find photo.jpg has already been deleted.

Canicula answered 29/11, 2013 at 6:25 Comment(9)
Doesn't work for me (iOS 7, Xcode 5). I keep getting a writing error and when I log the URL in the completion block it is null. (I am sure that my asset is editable, in fact, I have copypasted your code).Haslett
Hi Δ developer, have you added photo.jpg to your project? If not the image object before "writeImageToSavedPhotosAlbum:metadata:completionBlock:" should be nil, please check, thanks.Canicula
Hi, I'm doing it with a programmatically-generated image, which isn't nil (I checked that)Haslett
Please paste your code snippet here, thanks, I will look into it.Canicula
My creation code is yours with a programmatically-generated image which isn't nil (and it works because I can see the image in the photos album). My deletion code is exactly yours, copypasted from here.Haslett
Sorry, I tried your scenario, but it works both on iOS 6 and 7. I just programmatically generates an image and save to default gallery with above code, the image can be found in default gallery, then I use delete with [ALAsset setImageData:nil metadata:nil completionBlock:...], the image is deleted from default gallery as expected.Canicula
I also can't get this to work (iOS7) with the following error: (Error Error Domain=ALAssetsLibraryErrorDomain Code=-3300 "Write failed" UserInfo=0xd42d420 {NSLocalizedDescription=Write failed, NSUnderlyingError=0xd435ff0 "Write failed", NSLocalizedFailureReason=There was a problem writing this asset because the write failed.}) If I provide a new image (as NSData) it works fine though to replace, but I can't deleteVietnamese
For me, the deletion consistently fails in the simulator, but actually does work on the device.Fulfill
Works perfect on iOS 7. This should be the chosen answer.Bellamy
L
10

This is not possible using any documented API. Only the photos app can delete Albums. If you want this feature to be added to iOS, I would fill a feature request at https://feedbackassistant.apple.com/.

Linkous answered 15/6, 2012 at 22:28 Comment(0)
L
7

in ios8 deleting photos might be possible using the Photos Framework

Please check the documentation of Photos Framework

For deleting assets refer to PHAssetChangeRequest

+ (void)deleteAssets:(id<NSFastEnumeration>)assets

where assets is an array of PHAsset objects to be deleted.

For deleting collections refer to PHAssetCollectionChangeRequest

+ (void)deleteAssetCollections:(id<NSFastEnumeration>)assetCollections

https://developer.apple.com/library/prerelease/ios/documentation/Photos/Reference/PHAssetChangeRequest_Class/index.html#//apple_ref/occ/clm/PHAssetChangeRequest/deleteAssets:

Lunge answered 23/6, 2014 at 10:21 Comment(2)
Hi and welcome to Stack Overflow. Please note that while your answer remains here, the link might change or become unavailable. Please edit your post to contain the essentials of the link's content.Redvers
@Lunge Can you provide some sample code as to how to call it? Its not clear as to what to import and how to callSplotch
B
5

As Ted said, this is now possible in iOS 8 using the Photos service. It's pretty clean actually. You need to submit a change request to the photolibrary. Here's an example.

[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
    [PHAssetChangeRequest deleteAssets:arrayOfPHAssets];
} completionHandler:^(BOOL success, NSError *error) {
    NSLog(@"Finished deleting asset. %@", (success ? @"Success." : error));
}];

Make sure you've imported Photos, and gotten authorization from the user. (Which you probably did to show the image already)

PHAssetchangeRequest - deleteAssets https://developer.apple.com/library/prerelease/ios/documentation/Photos/Reference/PHAssetChangeRequest_Class/index.html#//apple_ref/occ/clm/PHAssetChangeRequest/deleteAssets: PHPhotoLibrary Class - authorizationStatus https://developer.apple.com/library/ios/documentation/Photos/Reference/PHPhotoLibrary_Class/#//apple_ref/occ/clm/PHPhotoLibrary/authorizationStatus

Byelaw answered 19/1, 2015 at 9:8 Comment(0)
B
2

evanchin is correct. Further more, if you want to do this in Xamarin.iOS (aka monotouch):

var lib = new ALAssetsLibrary();
lib.Enumerate(ALAssetsGroupType.All, (ALAssetsGroup group, ref bool libStop) =>
{
    if (group == null)
    {
        return;
    }
    group.Enumerate((ALAsset asset, int index, ref bool groupStop) =>
    {
        if (asset != null && asset.Editable)
        {
            asset.SetImageDataAsync(new NSData(IntPtr.Zero), new NSDictionary(IntPtr.Zero));
        }
    });
}, error => { });

This code will delete all images that your app added to the ALAssetsLibrary.

Bandage answered 27/4, 2014 at 15:20 Comment(0)
D
1

You may delete any asset in the library using documented API ONLY.

  1. over writing the [ALAsset isEditable] function:

    @implementation ALAsset(DELETE)
    -(BOOL)isEditable{
        return YES;
    }
    @end
    
  2. like evanchin said, delete the asset:

    ALAssetsLibrary *lib = [ALAssetsLibrary new];
    [lib enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos 
                       usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
        [group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop) {
            if(asset.isEditable) {
                [asset setImageData:nil 
                           metadata:nil 
                    completionBlock:^(NSURL *assetURL, NSError *error) {
                    NSLog(@"Asset url %@ should be deleted. (Error %@)", assetURL, error);
                }];
             }
         }];
     } failureBlock:^(NSError *error) {
    
     }];
    
Daylong answered 14/5, 2014 at 11:3 Comment(1)
getting this error: Asset url (null) should be deleted. (Error Error Domain=ALAssetsLibraryErrorDomain Code=-3301 "Write busy" UserInfo=0x17df6780 {NSLocalizedRecoverySuggestion=Try to write again, NSLocalizedFailureReason=There was a problem writing this asset because the writing resources are busy., NSLocalizedDescription=Write busy, NSUnderlyingError=0x17d1cae0 "Write busy"})Thesaurus

© 2022 - 2024 — McMap. All rights reserved.