xcode 在 Swift 中获取图像名称 UIImagePickerController
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30953070/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get image name UIImagePickerController in Swift
提问by Sergey Burd
I use UIImagePickerController to get image from phone library (iOS). After edit mode i put image to my UIImageView. I want to save this image in Core Data to use it in another View Controllers
我使用 UIImagePickerController 从手机库(iOS)中获取图像。编辑模式后,我将图像放入 UIImageView。我想将此图像保存在 Core Data 中以在另一个视图控制器中使用它
How can i do it in Swift. If it is not possible what options do i have to save image ?
我怎样才能在 Swift 中做到这一点。如果不可能,我有哪些选项可以保存图像?
回答by Dharmesh Kheni
Another good option is you can save your Image into Document Directory of your app and you can retrieve that image from anywhere like shown in below code:
另一个不错的选择是您可以将图像保存到应用程序的文档目录中,您可以从任何地方检索该图像,如下面的代码所示:
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
self.dismissViewControllerAnimated(true, completion: nil)
let tempImage = info[UIImagePickerControllerOriginalImage] as! UIImage
// save your image here into Document Directory
saveImage(tempImage, path: fileInDocumentsDirectory("tempImage"))
}
Here is the helper functions:
这是辅助函数:
func saveImage (image: UIImage, path: String ) -> Bool{
let pngImageData = UIImagePNGRepresentation(image)
//let jpgImageData = UIImageJPEGRepresentation(image, 1.0) // if you want to save as JPEG
let result = pngImageData.writeToFile(path, atomically: true)
return result
}
// Get the documents Directory
func documentsDirectory() -> String {
let documentsFolderPath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0] as! String
return documentsFolderPath
}
// Get path for a file in the directory
func fileInDocumentsDirectory(filename: String) -> String {
return documentsDirectory().stringByAppendingPathComponent(filename)
}
And this way you can retrieve that image from Document Directory:
通过这种方式,您可以从文档目录中检索该图像:
@IBAction func setImage(sender: AnyObject) {
imageV.image = loadImageFromPath(fileInDocumentsDirectory("tempImage"))
}
And here is the helper function:
这是辅助函数:
func loadImageFromPath(path: String) -> UIImage? {
let image = UIImage(contentsOfFile: path)
if image == nil {
println("missing image at: (path)")
}
println("\(path)") // this is just for you to see the path in case you want to go to the directory, using Finder.
return image
}
Hope It will help you.
希望它会帮助你。
回答by DHEERAJ
You can store images in Core Data using the Binary Data attribute type. Then change your image from the picker to NSData.
您可以使用二进制数据属性类型将图像存储在 Core Data 中。然后将您的图像从选择器更改为 NSData。
Then just save it like anything else.
然后像其他任何东西一样保存它。
However you should be aware of a few things:
但是,您应该注意以下几点:
Always convert your UIImage to a NSData Format
始终将您的 UIImage 转换为 NSData 格式
If you run into performance issues, try moving the Binary Data attribute to a separate entity.
如果遇到性能问题,请尝试将二进制数据属性移至单独的实体。
You should abstract the conversion to NSData behind the interface of your NSManagedObject subclass, so you don't have to worry about conversions from UIImage to NSData or vice versa.
你应该在你的 NSManagedObject 子类的接口后面抽象到 NSData 的转换,所以你不必担心从 UIImage 到 NSData 的转换,反之亦然。
If your images are not strongly related to the entities in your model, I would suggest storing them outside of Core Data.
如果您的图像与模型中的实体没有密切关系,我建议将它们存储在 Core Data 之外。
Example :
例子 :
func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: NSDictionary!){
self.dismissViewControllerAnimated(true, completion: nil)
newImageData = UIImageJPEGRepresentation(image, 1)
}
myImageFromData = UIImage(data: newImageData)