我有一个存储收据的应用程序。它的工作方式是,您使用应用程序保存图像,然后将其上传到 amazon s3 存储桶。要上传到 amazon s3,您需要其本地路径。
所以这就是我正在做的事情。
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")
}
路径看起来像这样 -
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
现在,如果将收据直接上传到 s3 存储桶,这将起作用。但是如果我关闭应用程序,然后重新加载它就不起作用。
我认为这是因为每次运行应用程序时 FE04FF78 都会发生变化。如果是这种情况,我如何获取图像的路径?