How to get path to photo taken with ios swift camera

I used the imagePickerController (_ picker: UIImagePickerController, didFinishPickingMediaWithInfo method to get the url of the image that the user selected in the photo gallery. But when I try to get the url for the image taken by the camera, its back null. 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)
            }
        }

      

+3


source to share


2 answers


The image you made is not saved and does not yet have a name. You need to save the image before you can get the image path. This is why it returns nil

.



If you pick an image from your image library instead of taking it with your camera, you get the way.

+1


source


After you have saved the image in the photo album, you can get the path in the callback callback



func saveImage(image: UIImage, completion: @escaping (Error?) -> ()) {
   UIImageWriteToSavedPhotosAlbum(image, self, #selector(image(path:didFinishSavingWithError:contextInfo:)), nil)
}

@objc private func image(path: String, didFinishSavingWithError error: NSError?, contextInfo: UnsafeMutableRawPointer?) {
    debugPrint(path) // That the path you want
}

      

+2


source







All Articles