Search code examples
iosswiftuiimagepickercontroller

how to get path of picture taken by camera ios swift


I was using the imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo delegate method for getting the url of the image which user chose on photo gallery. But when I try to get URL for image taken by camera its return nill. Here is my code.

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        if (imagePicker.sourceType == UIImagePickerControllerSourceType.camera) {
            let data = UIImagePNGRepresentation(pickedImage)
            UIImageWriteToSavedPhotosAlbum(pickedImage, nil, nil, nil)

            if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
                let imageURL =  info[UIImagePickerControllerReferenceURL] as? NSURL
                print(imageURL)
            }
        }

Solution

  • The image you have taken is not saved and has not any name yet. You need to save the image before you can get the path for the image. That´s why it returns nil.

    If you select an image from the image library instead of taking one with the camera you´ll get a path.