Delete an asset (picture or video) from IPhone in IOS
Asked Answered
M

5

9

I am working on an Iphone app and I can enumerate assets using the Assetslibrary and load them into a table view. The user can delete a row (a picture / video) in the app but how do I UPDATE the Iphone photo album directly from my app? Otherwise on refreshing, the tableview will reload the previously deleted asset.

Moschatel answered 26/8, 2012 at 1:30 Comment(0)
L
0

Possible duplication of https://mcmap.net/q/139216/-alassetslibrary-delete-alassetsgroup-alasset. Simple answer: you can't. The Photos app is the only place you can delete assets. Which is probably a good thing--you wouldn't want any willy-nilly app to be able to delete all your photos, would you?

Lettyletup answered 26/8, 2012 at 2:41 Comment(1)
Just wanted to be certain. Thank you.Moschatel
H
15

in ios8 deleting photos is 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/documentation/photos/phassetchangerequest/1624062-deleteassets
https://developer.apple.com/documentation/photos/phassetcollectionchangerequest/1619453-deleteassetcollections

Hartmann answered 23/6, 2014 at 9:46 Comment(2)
I'm not convinced that an app is able to delete assets from the photo library. I would like to see an example.Achromic
what if I have only image and image's local identifier like "AF508E2B-6B00-4E26-8A2B-B160C74478A9/L0/001". then how to delete image from custom album in gallery ?Hoey
J
11

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/documentation/photos/phassetchangerequest/1624062-deleteassets PHPhotoLibrary Class - authorizationStatus https://developer.apple.com/documentation/photos/phphotolibrary/1620745-authorizationstatus

Jowl answered 19/1, 2015 at 8:54 Comment(1)
what is arrayOfPHAssets ? I want to delete one single video from album. Can you help me ?Gildagildas
L
1

It late but for other users it will help.
As we know only the photos app can delete images. In such situation, i retrieved all images from photos via alassets, viewed them in custom gallery, given user a option to select multiple images from assets to save them in phone directory. Next i am using my app gallery instead of photos gallery. I have given the option in the app to import images from photos to app gallery (which is a document directory folder of images) at any time in the app.

Lepage answered 23/5, 2013 at 11:21 Comment(0)
C
1

Adding an answer to an old question here, because we are often asked for Screenshot prevention as part of a Data-Loss-Prevention (DLP) solution. You can (a) register for screenshot notifications and (b) ask the user to delete when it occurs, but there's no way to do it silently or secretly. Here's a full working code example:

func applicationDidBecomeActive(application: UIApplication) {
    registerForScreenShotNotifications()
}

func registerForScreenShotNotifications() {
   NSNotificationCenter.defaultCenter().addObserverForName(UIApplicationUserDidTakeScreenshotNotification, object: nil, queue: NSOperationQueue.mainQueue()) { (notification) in
        print("Yep they took a screenshot \(notification)")

        let assetToDelete = self.getLastImage()
        if  let assetToDelete = assetToDelete
        {
            PHPhotoLibrary.sharedPhotoLibrary().performChanges({
                PHAssetChangeRequest.deleteAssets([assetToDelete])
                }, completionHandler: { (success, error) in
                    print("Success \(success) - Error \(error)")
            })

        }
    }
}

// NOTE : You should ask for permission to access photos before this
func getLastImage() -> PHAsset? {
    let fetchOptions = PHFetchOptions()
    fetchOptions.sortDescriptors = [ NSSortDescriptor(key: "creationDate", ascending: true) ]
    let fetchResult = PHAsset.fetchAssetsWithMediaType(.Image, options: fetchOptions)
    let newestAsset = fetchResult.lastObject

    return newestAsset as! PHAsset?
}

The result is this:

ScreenShot Remover Sample Code Result

Connieconniption answered 3/8, 2016 at 19:2 Comment(0)
L
0

Possible duplication of https://mcmap.net/q/139216/-alassetslibrary-delete-alassetsgroup-alasset. Simple answer: you can't. The Photos app is the only place you can delete assets. Which is probably a good thing--you wouldn't want any willy-nilly app to be able to delete all your photos, would you?

Lettyletup answered 26/8, 2012 at 2:41 Comment(1)
Just wanted to be certain. Thank you.Moschatel

© 2022 - 2024 — McMap. All rights reserved.