iOS & Swift - How can I get and save a UIImage from the user?

quillford picture quillford · Nov 2, 2014 · Viewed 18.4k times · Source

I am writing a Swift app for iOS. I want to know how to get an image from the user's camera roll (or whatever they're calling it these days) and save it locally within the app so that I can reference it later. How would one go about this? As an example, let's say I want to get the image and set it as the image for a UIImageView in my storyboard.

Answer

Brian Tracy picture Brian Tracy · Nov 3, 2014

This is exactly what UIImagePickerController combined with NSUserDefaults will do.

For you it will be a two part task. First you will have to capture the image with a UIImagePickerController and then either store it in their photo library, or store it on their device with NSUserDefaults.

To store an image with NSUserDefaults, see this question.