如何在我的应用程序内保存图像(path)?

我想制作一个应用程序,用户填写某种forms,并附上图像。 用户从图库中选取图片(使用UIImagePickerController)。 我已经知道如何显示与info[UIImagePickerControllerOriginalImage] as? UIImage屏幕上的图像info[UIImagePickerControllerOriginalImage] as? UIImage info[UIImagePickerControllerOriginalImage] as? UIImage

我想创build一个文件,这个文件将在下次打开应用程序时使用,所以相同的图像可以和注释一起显示。 所以最好我想保存文件的path,所以下次我只是用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) } 

编辑

经过一番阅读,我发现没有办法获得保存在应用程序沙箱外部的图像的path。 所以大家都在寻找同样的东西,停下来,用Bluehound的答案。
有关如何在iOS(和OS X)中处理文件的更多信息,请参阅: https : //developer.apple.com/library/ios/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html

文件目录是存储图像的好地方。 您可以保存到它,然后从中检索图像。 我使用这些UIImage扩展来保存和检索图像:

 extension UIImage { func save(fileName: String, type: String) { let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String if type.lowercaseString == "png" { let path = "\(documentsPath)/\(fileName).\(type)" UIImagePNGRepresentation(self).writeToFile(path, atomically: true) } else if type.lowercaseString == "jpg" { let path = "\(documentsPath)/\(fileName).\(type)" UIImageJPEGRepresentation(self, 1.0).writeToFile(path, atomically: true) } else { } } convenience init?(fileName: String, type: String) { let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String let path = "\(documentsPath)\(fileName).\(type)" self.init(contentsOfFile: path) } } 

使用这个,我可以使用某些数据存储方法(如Core Data)来存储文件名,而不是整个图像的path。

使用:

 someImage.save("SomeName" , type: "png") if let image = UIImage(fileName: "SomeName", type: "png") { } 

这里是你如何保存图像

 - (IBAction)saveImage { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:@"savedImage.png"]; UIImage *image = imageView.image; // imageView is where you are displaying image after selecting one from UIImagePicker NSData *imageData = UIImagePNGRepresentation(image); [imageData writeToFile:savedImagePath atomically:NO]; } 

你可以像这样取回这个图像

 - (IBAction)getImage { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *getImagePath = [documentsDirectory stringByAppendingPathComponent:@"savedImage.png"]; UIImage *img = [UIImage imageWithContentsOfFile:getImagePath]; } 

更新:

我相信你问题的标题意味着有点不同。 回到你的问题,如果你打算一直只使用一个图像,你可以有一个单一的图像名称,并使用它。 这将随时覆盖先前保存的图像。

否则,另一个选项可能是创build一个简单的plist或db,并使用图像和时间戳来唯一标识每个图像。

Swift代码保存图像:

 let nsDocumentDirectory = NSSearchPathDirectory.DocumentDirectory let nsUserDomainMask = NSSearchPathDomainMask.UserDomainMask if let paths = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true) { if paths.count > 0 { if let dirPath = paths[0] as? String { let image = ***YOUR IMAGE*** let writePath = dirPath.stringByAppendingPathComponent("Image2.png") UIImagePNGRepresentation(image).writeToFile(writePath, atomically: true) } } } 

为了获得图像可以非常容易相关