swift - 如何保存图像及其路径,然后使用它来快速上传?

标签 swift swift3 s3transfermanager

我有一个存储收据的应用程序。它的工作方式是,您使用该应用程序保存图像,然后将其上传到亚马逊 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 都会发生变化。 如果是这种情况我如何获得图像的路径?

最佳答案

您应该只保存文件名并每次需要时获取文档路径并附加到您的文件名

因为每当应用程序的新实例启动时,文档路径都会发生变化

关于swift - 如何保存图像及其路径,然后使用它来快速上传?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44578656/

相关文章:

swift - 无法在 Swift 3 的 Collection 扩展中使用 indices.contains()

ios - S3TransferManager 无法在 ios 中的 S3GetObjectRequest targetedFilePath 上保存文件/数据

ios - 如何在 URL 查询中转义 & 符号?

ios - 如何根据输入的日期对核心数据数组进行排序

ios - 状态栏样式不会因单个 View 而改变 - Swift 2/iOS9

ios - 使用 Alamofire 的不可信证书。我已经尝试了所有我能找到的答案

swift - Swift 3.0 上没有这样的模块 'SwiftyJSON'

iOS Swift Alamofire JSON 响应返回 Bool 值 `1` 或 `0`