Search code examples
iosswiftuiimagepickercontroller

Get Image URL of photo with UIImagePickerControllerDelegate


I am using UIImagePickerController to select images. I was hoping to get the local path too.

I am doing this for images in an album using

info[UIImagePickerController.InfoKey.imageURL] as? URL

In the case of an image using the camera this is nil however.

Is it possible to capture this path using UIImagePickerControllerDelegate?


Solution

  • Yes, but with another key:

    info[UIImagePickerController.InfoKey.originalImage] as? UIImage
    

    or

    info[UIImagePickerController.InfoKey.editedImage] as? UIImage
    

    Please note that the object there will be UIImage type, not URL

    Camera photos are not saved by default and don't have local path. So you need to save it by yourself. E.g.:

    if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
        try? image.pngData()?.write( .... )
    }