Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift 3.0 Getting URL of UIImage selected from UIImagePickerController

Note:- This question is only for Swift 3.0 I am able to get the path prio to Swift 3.0

I want to get UIImage's path picked in didFinishPickingMediaWithInfo method

let imageUrl          = info[UIImagePickerControllerReferenceURL] as? NSURL
let imageName         = imageUrl.lastPathComponent
let documentDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
let photoURL          = NSURL(fileURLWithPath: documentDirectory)
let localPath         = photoURL.appendingPathComponent(imageName!)

But this path is not pointing to my image, Even there is no any image in Documents folder.

Can anyone help me on this?

like image 329
Mayank Jain Avatar asked Oct 28 '16 08:10

Mayank Jain


2 Answers

You can't access the path of the picked image directly. You need to save that in your DocumentsDirectory and then take the image back with the path.

Do this

Swift 3.x

 func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

    let image = info[UIImagePickerControllerOriginalImage] as! UIImage
    let imageUrl          = info[UIImagePickerControllerReferenceURL] as? NSURL
    let imageName         = imageUrl?.lastPathComponent
    let documentDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
    let photoURL          = NSURL(fileURLWithPath: documentDirectory)
    let localPath         = photoURL.appendingPathComponent(imageName!)

    if !FileManager.default.fileExists(atPath: localPath!.path) {
        do {
            try UIImageJPEGRepresentation(image, 1.0)?.write(to: localPath!)
            print("file saved")
        }catch {
            print("error saving file")
        }
    }
    else {
        print("file already exists")
    }
}

Also note that you are using the name as the last path component which is same for every file. So, this will save your image in DocumentDirectory only once as it will find the path next time.

Now when you access the localPath variable and navigate to the path, you will find the image.

NOTE:
If you are using a device here, then you need to download the container of the device, show its package contents and navigate to the Documents Directory where you will find your saved image.

like image 139
Rajan Maheshwari Avatar answered Nov 18 '22 12:11

Rajan Maheshwari


In SWIFT 4 U Can Try This, It's working properly.

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {


    if let imgUrl = info[UIImagePickerControllerImageURL] as? URL{
        let imgName = imgUrl.lastPathComponent
        let documentDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first
        let localPath = documentDirectory?.appending(imgName)

        let image = info[UIImagePickerControllerOriginalImage] as! UIImage
        let data = UIImagePNGRepresentation(image)! as NSData
        data.write(toFile: localPath!, atomically: true)
        //let imageData = NSData(contentsOfFile: localPath!)!
        let photoURL = URL.init(fileURLWithPath: localPath!)//NSURL(fileURLWithPath: localPath!)
        print(photoURL)

    }

    APPDEL.window?.rootViewController?.dismiss(animated: true, completion: nil)
}
like image 26
Jaydip Avatar answered Nov 18 '22 14:11

Jaydip