2017-06-15 10 views
0

我有一個存儲收據的應用程序。它的工作方式是,您使用應用程序保存圖像,然後將其上傳到亞馬遜S3存儲桶。要上傳到亞馬遜S3,你需要它的本地路徑。如何保存圖像及其路徑,然後稍後使用它使用swift進行上傳?

所以這就是我正在做的。

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每次運行應用程序都會被更改。 如果是這種情況,我如何獲得圖像的路徑?

+0

你應該只保存文件名並每次獲取文檔路徑,只要你需要並追加到你的文件名 –

+0

,因爲文檔目錄每次啓動一個新的應用程序實例時都會改變] –

+0

@UsamaSadiq如果你想發佈這個答案,我會將其標記爲正確的。謝謝,我幫助了我 – Nee

回答

0

你應該只保存文件名和獲取文件的路徑每次,只要你需要,並追加到您的文件名

由於文件路徑的每一次變化,每當應用程序的新實例啓動

0

如果需要,您只應將文件名或任何子文件夾保存在Documents之下。如果你只是從你的設備打開應用程序,那麼它應該工作得很好。但是,每次您的應用程序編譯(您從Xcode調試/運行應用程序)時,UUID(在本例中爲FE04FF78-914B-4224-A84B-D10521E0F845)都會更改。

相關問題