11

Is there a way I can delete an image that is loaded into my app from a UIImagePickerController?

I want to be able to delete the image from the user's photo library when the user performs a specific action.

I am prompting the user to choose a image from their library, then it gets loaded into my app at which point the app does some shnazzy animation, then actually deletes the image.

Please help!

Shaik Riyaz
  • 11,204
  • 7
  • 53
  • 70
Jacob Relkin
  • 161,348
  • 33
  • 346
  • 320

3 Answers3

16

Apple doesn't actually allow you to delete from the photo library through an API. The user has to actually go to the Photos app and delete it manually themselves. Apple does allow you write to the photo library:

To save a still image to the user’s Saved Photos album, use the UIImageWriteToSavedPhotosAlbum function. To save a movie to the user’s Saved Photos album, use the UISaveVideoAtPathToSavedPhotosAlbum function.

But for deleting and editing/overriding an existing photo, Apple doesn't have anything like that right now.

John Wang
  • 6,136
  • 2
  • 28
  • 23
12

Actually, you can delete photos saved by your app (saved to photo library with UIImageWriteToSavedPhotosAlbum API call).

The documented API [ALAsset setImageData:metadata:completionBlock:] works.

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.

Twitter khuong291
  • 11,328
  • 15
  • 80
  • 116
evanchin
  • 2,028
  • 1
  • 22
  • 25
  • It works :). But, setImageData:metaData:completionBlock function is used to replace an image, not delete it according to the Apple docs. Are you sure there won't be problems with the AppStore? – Sukhrob Apr 21 '14 at 08:09
  • 1
    I already have an app in Apple store, this is a public API, no problem in app review process. – evanchin Apr 25 '14 at 04:01
2

Yes we can delete a photo. We can use PHAssetChangeRequest for this operation.

From Apple:

A request to create, delete, change metadata for, or edit the content of a Photos asset, for use in a photo library change block.

class func deleteAssets(_ assets: NSFastEnumeration)

where assets: An array of PHAsset objects to be deleted.

PHAssetChangeRequest.deleteAssets([assetToDelete])

So, you could use the above code to delete assets.

below is swift 3 code,

PHPhotoLibrary.shared().performChanges({
            let imageAssetToDelete = PHAsset.fetchAssets(withALAssetURLs: imageUrls as! [URL], options: nil)
            PHAssetChangeRequest.deleteAssets(imageAssetToDelete)
        }, completionHandler: {success, error in
            print(success ? "Success" : error )
        })
Abdul Yasin
  • 3,480
  • 1
  • 28
  • 42
  • PHAsset.fetchAssets(withALAssetURLs:) is deprecated and i don't see another approach to delete a photo starting from an URL – Gabbr Issimo Jan 18 '23 at 12:09