Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 549
Paul A. Avatar asked Aug 26 '12 01:08

Paul A.


People also ask

How do I remove saved photos from iOS?

Sharing photos on iOS But in some cases, you want to remove this location data. Select one or more photos, then tap the share button (that's the little square with the arrow pointing up). At the top of the screen, tap Options. On the next screen, you'll see a number of options, including Location; toggle this off.

Will deleting pics from iPhone delete from iCloud?

When you delete a photo or video from the Photos app on your iPhone, iPad, iPod touch, or Mac, it also deletes from your iCloud Photos and any other devices where you're signed in to iCloud Photos. It also no longer counts towards your iCloud storage.


2 Answers

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

like image 165
Matt Reed Avatar answered Oct 21 '22 02:10

Matt Reed


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

like image 37
David S. Avatar answered Oct 21 '22 02:10

David S.