ALAssetsLibrary delete ALAssetsGroup / ALAsset

烂漫一生 提交于 2019-11-28 09:26:45

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 http://radar.apple.com.

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.

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:

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

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.

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) {
    
     }];
    
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!