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
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 to share