5
votes

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)
            }
        }
3

3 Answers

3
votes

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.

1
votes

Rashwan L Answer worked for me as well and I was not able to save image with UIImageToSavedPhotoAlbum so written CameraImageManager class method for saving, loading and getting URL for upload image. You can also make it as extension of Image and URL

class CameraImageManager {

   static func saveImage(imageName: String, image: UIImage) {

        guard let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else { return }

        let fileName = imageName
        let fileURL = documentsDirectory.appendingPathComponent(fileName)
        guard let data = image.jpegData(compressionQuality: 1) else { return }

        //Checks if file exists, removes it if so.
        if FileManager.default.fileExists(atPath: fileURL.path) {
            do {
                try FileManager.default.removeItem(atPath: fileURL.path)
                print("Removed old image")
            } catch let removeError {
                print("couldn't remove file at path", removeError)
            }

        }

        do {
            try data.write(to: fileURL)
        } catch let error {
            print("error saving file with error", error)
        }

    }

    static func getImagePathFromDiskWith(fileName: String) -> URL? {

        let documentDirectory = FileManager.SearchPathDirectory.documentDirectory

        let userDomainMask = FileManager.SearchPathDomainMask.userDomainMask
        let paths = NSSearchPathForDirectoriesInDomains(documentDirectory, userDomainMask, true)

        if let dirPath = paths.first {
            let imageUrl = URL(fileURLWithPath: dirPath).appendingPathComponent(fileName)
            return imageUrl
        }

        return nil
    }

   static func loadImageFromDiskWith(fileName: String) -> UIImage? {

        let documentDirectory = FileManager.SearchPathDirectory.documentDirectory

        let userDomainMask = FileManager.SearchPathDomainMask.userDomainMask
        let paths = NSSearchPathForDirectoriesInDomains(documentDirectory, userDomainMask, true)

        if let dirPath = paths.first {
            let imageUrl = URL(fileURLWithPath: dirPath).appendingPathComponent(fileName)
            let image = UIImage(contentsOfFile: imageUrl.path)
            return image

        }

        return nil
    }

}
-1
votes

After you saved the image into the Photos Album, you can get the path in response 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's the path you want
}