[英]How to save an image and its path and then use it later to upload using swift?
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.
它的工作方式是:使用该应用保存图像,然后将其上传到Amazon s3存储桶。 To upload to amazon s3, you need its local path.
要上传到Amazon s3,您需要其本地路径。
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. 现在,如果将收据直接上传到s3存储桶,则可以使用。 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. 我认为这是因为每次您运行该应用程序时,FE04FF78都会被更改。 If that's the case how do I get the path of the image?
如果是这样,我如何获取图像的路径?
You should only save your file name or any sub-folder under Documents
if needed. 您仅应在需要时将文件名或任何子文件夹保存在“
Documents
下。 If you just open the app from your device then it should work fine. 如果您只是从设备上打开应用程序,那么它应该可以正常工作。 However, every time your app compiles (you debug/run the app from Xcode), the UUID, in this case
FE04FF78-914B-4224-A84B-D10521E0F845
, will change. 但是,每次您的应用程序编译(从Xcode调试/运行应用程序)时,UUID(在这种情况下为
FE04FF78-914B-4224-A84B-D10521E0F845
)都会更改。
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 由于每次启动新的应用程序实例时,文档路径都会更改
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.