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()
}
The photo library APIs from UIImagePickerController are deprecated.
storyboard file and add two elements inside the view controller. The first one is a button, which will show up an image picker modal whenever it gets pressed. The second one is an image view. I'm going to put a placeholder image at first, but once the user selected an image, we'll display that image to that view.
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 )
})
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()
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With