Reputation: 1595
I'm building an iOS app using Swift and Xcode 6. I have implemented the functionality to pick image from UIImagePickerController and set into UIImageView.
Here is my to code select image from UIImagePickerController:
@IBAction func loadImageButtonTapped(sender: UIButton) {
imagePicker.allowsEditing = false
imagePicker.sourceType = .PhotoLibrary
// imagePicker.sourceType = .Camera
//imagePicker.sourceType = .SavedPhotosAlbum
presentViewController(imagePicker, animated: true, completion: nil)
}
// MARK: - UIImagePickerControllerDelegate Methods
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
// imageView.contentMode = .ScaleAspectFit
imageView.image = pickedImage
}
dismissViewControllerAnimated(true, completion: nil)
}
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
dismissViewControllerAnimated(true, completion: nil)
}
Now i'm finding a best way to store this image and when user come again in the app, i can get the image and display in the uiimageview.
Could some help me how can i implement this.
Help is appreciated!
Thanks in Advance!
Upvotes: 1
Views: 987
Reputation: 3063
Try this.
let imageData: NSData = UIImageJPEGRepresentation(yourImage, 0.9)
let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String
let destinationPath = documentsPath.stringByAppendingPathComponent("filename.jpg")
imageData.writeToFile(destinationPath, atomically: false)
Then later to retrieve the image:
let loadedImage = UIImage(contentsOfFile: destinationPath)
Upvotes: 1