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.
-
Look up ALAssetsLibrary – Abizern Nov 02 '14 at 23:04
-
i think this link will help you a lot [check here][1] [1]: http://stackoverflow.com/questions/25730830/how-to-get-only-images-in-the-camera-roll-using-photos-framework – Saurabh Prajapati Nov 03 '14 at 09:58
5 Answers
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.

- 1
- 1

- 6,801
- 2
- 33
- 48
-
12
-
It's still a bad idea. You could just store the images in the local sandbox and refer to them with a more appropriate persistence mechanism. – Abizern Nov 02 '14 at 23:05
-
1
Here is my combination.
Saving Image:
let imageData = NSData(data:UIImagePNGRepresentation(pickedimage))
let paths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)
var docs: String = paths[0] as! String
let fullPath = docs.stringByAppendingPathComponent("yourNameImg.png")
let result = imageData.writeToFile(fullPath, atomically: true)
print(fullPath)
Get Image:
let nsDocumentDirectory = NSSearchPathDirectory.DocumentDirectory
let nsUserDomainMask = NSSearchPathDomainMask.UserDomainMask
if let paths = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true)
{
if paths.count > 0
{
if let dirPath = paths[0] as? String
{
let readPath = dirPath.stringByAppendingPathComponent("yourNameImg.png")
let image = UIImage(contentsOfFile: readPath)
UploadImagePreview.image = image
}
}
}
We can try with Photos Framework to fetch the last saved image.
var fetchOptions: PHFetchOptions = PHFetchOptions()
fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: true)]
var fetchResult = PHAsset.fetchAssetsWithMediaType(PHAssetMediaType.Image, options: fetchOptions)
if (fetchResult.firstObject != nil) {
var lastAsset: PHAsset = fetchResult.lastObject as! PHAsset
PHImageManager.defaultManager().requestImageForAsset(lastAsset, targetSize: self.UploadImagePreview.bounds.size, contentMode: PHImageContentMode.AspectFill, options: PHImageRequestOptions(), resultHandler: { (result, info) -> Void in
self.UploadImagePreview.image = result
})
}

- 9,564
- 146
- 81
- 122

- 14,148
- 92
- 64
-
@ Rodrigo: It works only when a complete path of the image is given. – Alvin George Jan 25 '16 at 06:17
Ray Wenderlich has a great tutorial for using a UIImagePickerController
to select an image and then using the AssetsLibrary
to save to an album.
There is also info on using image filters that is more than you asked for in the tutorial.

- 5,629
- 3
- 36
- 48

- 19,348
- 7
- 46
- 53
-
The only way I know to not conform to the UIImagePickerControllerDelegate is to not also conform to UINavigationControllerDelegate. The UIPicker needs a nav controller. – Steve Rosenberg Nov 03 '14 at 02:51
Once you get the UIImage object, you can save the image in a doc dir with the following code
let imageData = NSData(data:UIImagePNGRepresentation(wineImage.image))
let paths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)
var docs: String = paths[0] as! String
let fullPath = docs.stringByAppendingPathComponent("yourNameImg.png")
let result = imageData.writeToFile(fullPath, atomically: true)
You should definitely use a UIImagePickerController
as Brian mentioned, once you have the selected image use this file manager approach. I don't recommend using NSUserDefaults
to store images as it goes against Apple guidelines:
let imageData = UIImagePNGRepresentation(selectedImage)
let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String
let imagePath = paths.stringByAppendingPathComponent("cached.png")
if !imageData.writeToFile(imagePath, atomically: false)
{
println("not saved")
} else {
println("saved")
NSUserDefaults.standardUserDefaults().setObject(imagePath, forKey: "imagePath")
}

- 639
- 6
- 13