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)
}
}
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.
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
}
来源:https://stackoverflow.com/questions/44522756/how-to-get-path-of-picture-taken-by-camera-ios-swift