10

I need to delete the image from PhotoLibrary. I am using UIImagePickerController in my application to pick up the image. I need to delete this image from iOS PhotoLibrary after i use it in my application.

My Code snippet

if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.PhotoLibrary)
            {
                var imagePicker = UIImagePickerController()
                imagePicker.delegate = self
                imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary;
                imagePicker.allowsEditing = false
                self.presentViewController(imagePicker, animated: true, completion: nil)
            }


// MARK:- UIImagePickerControllerDelegate
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
    pickedImage = image
    saveImageToDisk(pickedImage)
/*
I need the logic to delete this image from PhotoLibrary here.
*/
    self.dismissViewControllerAnimated(true, completion: nil)
    refreshCollectionView()
}
Dinesh Jeyasankar
  • 1,043
  • 3
  • 10
  • 24

4 Answers4

8

Thanks for the help.

Fixed it with the below code.

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
    let image = info[UIImagePickerControllerOriginalImage] as! UIImage
    pickedImage = image
    saveImageToDisk(pickedImage)
    refreshCollectionView()
    let imageUrl = info[UIImagePickerControllerReferenceURL] as! NSURL
    var imageUrls = [imageUrl]
    //Delete asset 
    PHPhotoLibrary.sharedPhotoLibrary().performChanges( {
        let imageAssetToDelete = PHAsset.fetchAssetsWithALAssetURLs(imageUrls, options: nil)
        PHAssetChangeRequest.deleteAssets(imageAssetToDelete)
        },
        completionHandler: { success, error in
            NSLog("Finished deleting asset. %@", (success ? "Success" : error))
    })
    self.dismissViewControllerAnimated(true, completion: nil)
    refreshCollectionView()
}
Dinesh Jeyasankar
  • 1,043
  • 3
  • 10
  • 24
8

Just to add to the above, For swift 3.0 this worked for me.

PHPhotoLibrary.shared().performChanges({
                let imageAssetToDelete = PHAsset.fetchAssets(withALAssetURLs: imageUrls as! [URL], options: nil)
                PHAssetChangeRequest.deleteAssets(imageAssetToDelete)
            }, completionHandler: {success, error in
                print(success ? "Success" : error )
            })
Sri Hari YS
  • 146
  • 2
  • 1
6

Get image url....

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
    let imageURL = info[UIImagePickerControllerReferenceURL] as NSURL
    let imageName = imageURL.path!.lastPathComponent


    picker.dismissViewControllerAnimated(true, completion: nil)

}

delete the asset:

PHPhotoLibrary.sharedPhotoLibrary().performChanges( {
    let imageAssetToDelete = PHAsset.fetchAssetsWithALAssetURLs(**imageUrl**, options: nil)
    PHAssetChangeRequest.deleteAssets(imageAssetToDelete)
},
    completionHandler: { success, error in
        NSLog("Finished deleting asset. %@", (success ? "Success" : error))
})
Rahul Mayani
  • 3,761
  • 4
  • 25
  • 40
1

You can do it in following way:

//under SamplePhotosApp/AAPLAssetViewController.m

// Delete asset from library
 [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
[PHAssetChangeRequest deleteAssets:@[self.asset]];
} completionHandler:completionHandler];

where self.asset is a PHAsset object (which can be acquired in several ways) referring to the photo you wish to delete. Don't forget to import the Photos framework!

Hope this helps!

pankaj asudani
  • 862
  • 6
  • 18