Reputation: 2315
on my SwiftUI project I need to use the url of the image to be reused in other part of the project.
I have created this image picker that allow me to choose between phone album or take a new photo with the iPhone camera (with the var shootNew: Bool)
I facing the following issue: when I use the picker to select the image from the photo album im getting the url of the image, but when I want to take a new picture with the camera I can't get the url of the new picture,
How can I get url of the picture taken with camera?
here my imagePicker
struct ImagePickerNew: UIViewControllerRepresentable {
var shootNew: Bool
var needEdit: Bool
@Binding var image: UIImage?
@Binding var isPresented: Bool
@Binding var imageUrl : URL?
class Coordinator: NSObject, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
var shootNew: Bool
var needEdit: Bool
@Binding var image: UIImage?
@Binding var isPresented: Bool
@Binding var imageUrl : URL?
init(shootNew: Bool, needEdit: Bool, image: Binding<UIImage?>, isPresented:Binding<Bool>, imageURL1 : Binding<URL?>) {
self.shootNew = shootNew
self.needEdit = needEdit
_image = image
_isPresented = isPresented
_imageUrl = imageURL1
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let imageEdited = info[UIImagePickerController.InfoKey.editedImage] as? UIImage {
self.image = imageEdited
} else {
self.image = (info[UIImagePickerController.InfoKey.originalImage] as! UIImage)
}
if let imageURLEdited = info[UIImagePickerController.InfoKey.imageURL] as? URL{
self.imageUrl = imageURLEdited
debugPrint("ho url")
} else {
debugPrint("UNABLE URL")
}
self.isPresented = false
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
self.isPresented = false
}
}
func makeCoordinator() -> Coordinator {
return Coordinator(shootNew: shootNew, needEdit: needEdit, image: $image, isPresented: $isPresented, imageURL1: $imageUrl)
}
func makeUIViewController(context: UIViewControllerRepresentableContext<ImagePickerNew>) -> UIImagePickerController {
let picker = UIImagePickerController()
picker.delegate = context.coordinator
picker.navigationController?.delegate = context.coordinator
picker.sourceType = shootNew ? .camera : .photoLibrary
picker.allowsEditing = needEdit
picker.mediaTypes = [kUTTypeImage] as [String]
return picker
}
func updateUIViewController(_ uiViewController: UIImagePickerController,
context: UIViewControllerRepresentableContext<ImagePickerNew>) {
}
}
Thanks a lot for helping me.
Upvotes: 0
Views: 1534
Reputation: 117
you can't get the URL of the photo taken by the camera because it is not written to the disk yet, you should write the photo data to the temporary documents folder first and then get the URL. Here is an example:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
var selectedImage: UIImage!
var imageUrl: URL!
if let image = info[UIImagePickerController.InfoKey.editedImage] as? UIImage {
selectedImage = image
} else if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
selectedImage = image
}
if picker.sourceType == UIImagePickerController.SourceType.camera {
let imgName = "\(UUID().uuidString).jpeg"
let documentDirectory = NSTemporaryDirectory()
let localPath = documentDirectory.appending(imgName)
let data = selectedImage.jpegData(compressionQuality: 0.3)! as NSData
data.write(toFile: localPath, atomically: true)
imageUrl = URL.init(fileURLWithPath: localPath)
} else if let selectedImageUrl = info[UIImagePickerController.InfoKey.imageURL] as? URL {
imageUrl = selectedImageUrl
}
self.mainImageView.image = selectedImage
MediaAPI.uploadMedia(fileUrl: imageUrl)
self.dismiss(animated: true, completion: nil)
}
Upvotes: 4