I going to fetch directory of photo From UIImagepicker using delegates method. but how to do this i don't know so give me hint for this isssue
Asked
Active
Viewed 61 times
1
-
Do you need path of your photos? – Sour LeangChhean Jun 26 '17 at 09:01
-
Yes @SourLeangChhean – Sagar vaishnav Jun 26 '17 at 09:03
-
Possible duplicate of [Getting URL of UIImage selected from UIImagePickerController](https://stackoverflow.com/questions/28255789/getting-url-of-uiimage-selected-from-uiimagepickercontroller) – Vlad Pulichev Jun 26 '17 at 09:10
-
yes but that code not working m working on swift 3.1 and xcode 8.3 – Sagar vaishnav Jun 26 '17 at 09:16
1 Answers
0
This line you can get the path of your photo in swift 3++:
let imageUrl = info[UIImagePickerControllerReferenceURL] as? NSURL
You can use it in delegate function func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
Code:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let image = info[UIImagePickerControllerOriginalImage] as! UIImage
// get image url
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")
}
}

Sour LeangChhean
- 7,089
- 6
- 37
- 39
-
-
-
-
-
@SourLeangChhean which object give me a path of selected image ? – Sagar vaishnav Jun 26 '17 at 09:22
-
let imageUrl = info[UIImagePickerControllerReferenceURL] as? NSURL – Sour LeangChhean Jun 26 '17 at 09:25