我想制作一个应用程序,用户填写某种表单并将图像附加到该应用程序。用户从图库中选取图片(使用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