I have an iPhone application using a UIImagePickerController
. As sourceType
I have
- UIImagePickerControllerSourceTypePhotoLibrary
- UIImagePickerControllerSourceTypeCamera
- UIImagePickerControllerSourceTypeSavedPhotosAlbum
so the user can make a photo or select one from the photo library from the photos or camera photos.
The image will be displayed in a UIImageView. The image should be saved if the user closes the app. So for text fields, I use NSUserDefaults
. I know, it is not a good way to save the image inside the NSUSerDefaults with NSData, so I want to save / copy the image to a folder which is controlled by my application similar to the NSUserDefaults.
How can I do this? I want to save it, and then I can write the path to the file into my NSUserDefaults
and read it on startup of the application.
Thank you in advance & Best Regards.
This is the code to save the UIImage into the document directory. You can use this code in didFinishPickingImage delegate method:
You can also use:
to find the path to your application document directory, instead of NSHomeDierctory.
You can use the following code in
UIImagePickerControllerDelegate
delegate implementationThat's all
Depending on the file format you want to save, you can use
OR
Update knuku's answer for Swift 3.0
here the image is saved as jpeg but you can also save it as png. the 0.0 parameter stands for compression and it's the lowest quality, if you want to get the best use 1.0.