I have an app which stores receipts. The way it works is, You save an image using the app and then you upload it to amazon s3 bucket. To upload to amazon s3, you need its local path.
So here is what I'm doing.
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
receiptImageView.image = image
Model.sharedModel.currentImage = image
print(info)
}
self.dismiss(animated: true, completion: nil)
//MAking the localpath of the image
let imageAWSName = "ios_" + NSUUID().uuidString + ".jpg"
let image = info[UIImagePickerControllerOriginalImage] as! UIImage
let documentDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
let photoURL = NSURL(fileURLWithPath: documentDirectory)
let localPath = photoURL.appendingPathComponent(imageAWSName)
if !FileManager.default.fileExists(atPath: localPath!.path) {
do {
try UIImageJPEGRepresentation(image, 1.0)?.write(to: localPath!)
Model.sharedModel.currentlocalpath = localPath
print("file saved")
}catch {
print("Error saving receipt photo local path")
}
}
else {
print("Error - localpath already exists")
}
The path looks like this -
file:///Users/Al/Library/Developer/CoreSimulator/Devices/FE04FF78-914B-4224-A84B-D10521E0F845/data/Containers/Data/Application/115C6A1B-E61F-4082-AA59-4674D87DF31E/Documents/ios_5AB04F97-32C9-441E-97C8-44D4D2725ADD.jpg
Now this works if upload the receipt straight away to s3 bucket. But if I close the app, and then reload it doesn't work.
I think it's because FE04FF78 gets changed everytime you run the app. If that's the case how do I get the path of the image?
you should only save filename and fetch the document path everytime , whenever you need and append to your filename
Because document path changes every time, whenever a new instance of app launches