我想製作一個應用程序,用戶填寫某種表單並將圖像附加到該應用程序。用戶從圖庫中選取圖片(使用UIImagePickerController)。我已經知道如何使用info[UIImagePickerControllerOriginalImage] as? UIImage
在屏幕上顯示圖像。如何在我的應用程序中保存圖像(路徑)?
我想製作一個文件,下次打開該應用程序時使用,因此可以在筆記中顯示相同的圖像。所以最好我想保存文件的路徑,所以下次我只需打開它UIImage(contentsOfFile: path)
。我怎樣才能得到這條道路?
有些不那麼有趣的代碼,我到目前爲止有:
@IBAction func chooseFromGallery(sender: AnyObject) {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.SavedPhotosAlbum){
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.SavedPhotosAlbum;
imagePicker.allowsEditing = false
self.presentViewController(imagePicker, animated: true, completion: nil)
}
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
dismissViewControllerAnimated(true, completion: nil)
self.image.image = info[UIImagePickerControllerOriginalImage] as? UIImage // This works
self.imagePath = // What goes here?
println(self.imagePath)
}
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
dismissViewControllerAnimated(true, completion: nil)
}
編輯
經過一番閱讀中,我發現,沒有得到的路徑圖像的方式被保存在應用程序沙箱外部。所以大家都在尋找相同的東西,停下來,並用Bluehound
的答案。
有關文件的方式中的iOS(和OS X)處理的更多信息:https://developer.apple.com/library/ios/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html
你到底用什麼樣的代碼?我面臨同樣的問題?我喜歡將圖像名稱存儲在覈心數據中,並將圖像放在同一個文件夾或類似的地方 – alex 2015-10-22 14:56:00