IOS & Swift - How can I get and save UIImage from user?

I am writing a Swift app for iOS. I want to know how to get an image from a custom camera roll (or whatever he calls it these days) and save it locally in the application so that I can refer to it later. How can I do that? For example, let's say I want to get an image and set it as an image for a UIImageView in my storyboard.

+8
ios swift xcode6 uiimage
source share
5 answers

This is exactly what UIImagePickerController is combined with NSUserDefaults .

For you, this will be a two-part task. First you will need to capture the image using the UIImagePickerController , and then save it to your photo library or save it to your device using NSUserDefaults .

To save the image using NSUserDefaults , see this question .

+4
source share

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 using the Photos Framework to get 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 }) } 
+3
source share

Ray Wenderlich has a great tutorial for using the UIImagePickerController to select an image, and then using the AssetsLibrary to save to an album.

There is also information about using image filters, which is more than what you requested in the tutorial.

+2
source share

Once you get the UIImage object, you can save the image in the doc directory 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) 
+2
source share

You should definitely use the UIImagePickerController , as Brian mentioned, once you use the selected image, use this approach to the file manager. I do not recommend using NSUserDefaults for storing images compared to the Apple Guide :

 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") } 
0
source share

All Articles