I have an app where the user has the ability to choose if they would like to take a picture or select one from their library. The code I have gets the image URL for the Xcode device simulator but does not work for my physical iPhone. When I choose an image using my personal device My result prints
Failed: There is a client side validation error for the field [local] with message: The file located at the `local` URL is missing. Make sure the file exists before uploading to storage.
But when I run with the device simulator it runs perfect and the image is stored in my backend. I am unsure as to why this is happening.
I would like to be able to get the URL from my own device.
printed url from xcode device simulator
file:///Users/GBMR/Library/Developer/CoreSimulator/Devices/877A8184-D857-4211-94B1-00A6B724A956/data/Containers/Data/Application/094279FA-37B1-45E6-ABC4-ADAA08B5477B/PicturesB71286E4-1994-4D76-AC14-D40A062BC832.jpeg
Completed: ywassupo
printed url from my physical iPhone
file:///var/mobile/Containers/Data/Application/C0415B3C-F50E-4D20-8D8A-941D29B9C4D1/Pictures9BA481F9-C52A-40FD-8A06-AAA2D6CDA6D7.jpeg
Failed: There is a client side validation error for the field [local] with message: The file located at the `local` URL is missing. Make sure the file exists before uploading to storage.
Code:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let selectedImage = info[UIImagePickerController.InfoKey.editedImage] as? UIImage {
imageView.image = selectedImage
imageView.contentMode = .scaleAspectFit
// detect(image: ciimage)
navigationItem.title = "Tap next to continue"
nextButton.isEnabled = true
if let imgUrl = info[UIImagePickerController.InfoKey.imageURL] as? URL{
let imgName = imgUrl.lastPathComponent
let documentDirectory = NSSearchPathForDirectoriesInDomains(.picturesDirectory, .allDomainsMask, true).first
let localPath = documentDirectory?.appending(imgName)
//let image = info[UIImagePickerController.InfoKey] as! UIImage
let data = selectedImage.pngData()! as NSData
data.write(toFile: localPath!, atomically: true)
//let imageData = NSData(contentsOfFile: localPath!)!
let photoURL = URL.init(fileURLWithPath: localPath!)
let infoTVC = InfoTableViewController()
infoTVC.imageChosenName = photoURL
_ = Amplify.Storage.uploadFile(key: "ywassupo", local: photoURL ,resultListener: {(event) in
switch event{
case .success(let data):
print("Completed: \(data)")
case .failure(let storageError):
print("Failed: \(storageError.errorDescription). \(storageError.recoverySuggestion)")
}
})
//NSURL(fileURLWithPath: localPath!)
print(photoURL)
dismiss(animated:true, completion: nil)
}
}
}
Can anyone steer me into the right direction as to how I can fix this?