How to save UIImage to a file?

If I have a UIImage from imagePicker, how can I save it to a subfolder in the document directory?

+92
ios objective-c iphone uiimage
Jul 21 2018-12-12T00:
source share
8 answers

Of course, you can create subfolders in the document folder of your application. You use the NSFileManager for this.

You use UIImagePNGRepresentation to convert the image to NSData and save it to disk.

 // Create path. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"Image.png"]; // Save image. [UIImagePNGRepresentation(image) writeToFile:filePath atomically:YES]; 

Core Data has nothing to do with saving images to disk.

+121
Jul 21 2018-12-12T00:
source share

In Swift 3:

 // Create path. let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true) let filePath = "\(paths[0])/MyImageName.png" // Save image. UIImagePNGRepresentation(image)?.writeToFile(filePath, atomically: true) 
+25
Dec 29 '15 at 1:30
source share

You must create a representation of your image as a specific format (say, JPEG or PNG), and then call writeToFile:atomically: in the view:

 UIImage *image = ...; NSString *path = ...; [UIImageJPEGRepresentation(image, 1.0) writeToFile:path atomically:YES]; 
+24
Jul 21 '12 at 12:50
source share

The above are useful, but they do not answer your question about how to save in a subdirectory or get an image from UIImagePicker.

First, you must indicate that your controller implements the image selection delegate in the .m or .h file, for example:

 @interface CameraViewController () <UIImagePickerControllerDelegate> @end 

Then you implement the imagePickerController delegate: didFinishPickingMediaWithInfo method: where you can get the photo using the image picker and save it (of course, you may have another class / object that handles the save, but I just show the code inside the method):

 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { // get the captured image UIImage *image = (UIImage *)info[UIImagePickerControllerOriginalImage]; NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; NSString *imageSubdirectory = [documentsDirectory stringByAppendingPathComponent:@"MySubfolderName"]; NSString *filePath = [imageSubdirectory stringByAppendingPathComponent:@"MyImageName.png"]; // Convert UIImage object into NSData (a wrapper for a stream of bytes) formatted according to PNG spec NSData *imageData = UIImagePNGRepresentation(image); [imageData writeToFile:filePath atomically:YES]; } 

If you want to save the image in JPEG format, the last 3 lines will be as follows:

 NSString *filePath = [imageSubdirectory stringByAppendingPathComponent:@"MyImageName.jpg"]; // Convert UIImage object into NSData (a wrapper for a stream of bytes) formatted according to JPG spec NSData *imageData = UIImageJPEGRepresentation(image, 0.85f); // quality level 85% [imageData writeToFile:filePath atomically:YES]; 
+14
Oct 02 '13 at
source share
 extension UIImage { /// Save PNG in the Documents directory func save(_ name: String) { let path: String = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first! let url = URL(fileURLWithPath: path).appendingPathComponent(name) try! UIImagePNGRepresentation(self)?.write(to: url) print("saved image at \(url)") } } // Usage: Saves file in the Documents directory image.save("climate_model_2017.png") 
+10
Jun 08 '17 at 12:35 on
source share
 NSData *imageData = UIImagePNGRepresentation(image); [imageData writeToFile:path atomically:YES]; 

where path is the name of the file you want to write.

+6
Jul 21 2018-12-12T00:
source share

You must first get the Documents directory.

 /* create path to cache directory inside the application Documents directory */ NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"fileName"]; 

Then you must save the photo in a file

 NSData *photoData = UIImageJPEGRepresentation(photoImage, 1); [photoData writeToFile:filePath atomically:YES]; 
+3
Jul 21 2018-12-12T00:
source share

In Swift 4:

 // Create path. let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask) if let filePath = paths.first?.appendingPathComponent("MyImageName.png") { // Save image. do { try UIImagePNGRepresentation(image)?.write(to: filePath, options: .atomic) } catch { // Handle the error } } 
+1
May 31 '18 at
source share



All Articles