Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete an image from PhotoLibrary after i pick it up using UIImagePickerController

Tags:

xcode

ios

swift

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()
}
like image 950
Dinesh Jeyasankar Avatar asked May 01 '15 05:05

Dinesh Jeyasankar


People also ask

Is UIImagePickerController deprecated?

The photo library APIs from UIImagePickerController are deprecated.

How do I use image picker in Swift?

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.


2 Answers

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 )
            })
like image 104
Sri Hari YS Avatar answered Sep 30 '22 15:09

Sri Hari YS


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()
}
like image 38
Dinesh Jeyasankar Avatar answered Sep 30 '22 13:09

Dinesh Jeyasankar