Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)
            }
        }
like image 927
Sarath Avatar asked Oct 18 '22 11:10

Sarath


2 Answers

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.

like image 123
Rashwan L Avatar answered Oct 20 '22 23:10

Rashwan L


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
    }

}
like image 24
Shruthi Palchandar Avatar answered Oct 21 '22 00:10

Shruthi Palchandar