Get UIImagePickerController Image Name in Swift

I am using UIImagePickerController to get the image from the phone library (iOS). After editing the mode, I placed the image in my UIImageView. I want to save this image in Core Data to use it in other View controllers

How can I do this in Swift. If it is not possible, what parameters do I need to save?

+1
source share
2 answers

Another good option is that you can save your image in the Document Directory of your application, and you can get this image from anywhere, as shown below:

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 are 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 so you can get this image from the document catalog:

 @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 this helps you.

+10
source

You can store images in the underlying data using the Binary Data attribute type. Then change the image from the collector to NSData.

Then just save it, like everything else.

However, you should know a few things:

Always convert your UIImage to NSData format p>

If you encounter performance issues, try moving the Binary Data attribute to a separate object.

You must abstract the conversion to NSData behind the interface of your NSManagedObject subclass, so you don’t have to worry about converting from UIImage to NSData or vice versa.

If your images are not strongly related to objects in your model, I would suggest saving them outside of 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) 
0
source

All Articles