Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save UIImage to file with NSFileManager?

Tags:

How I can save UIImage to file with NSFileManager ?

Thank,

like image 228
user1234096 Avatar asked Apr 10 '12 18:04

user1234096


People also ask

How do you save in UIImage?

If you've generated an image using Core Graphics, or perhaps rendered part of your layout, you might want to save that out as either a PNG or a JPEG. Both are easy thanks to two methods: pngData() and jpegData() , both of which convert a UIImage into a Data instance you can write out.

How do you add a UIImage in Objective C?

UIImage *img = [[UIImage alloc] init]; [img setImage:[UIImage imageNamed:@"anyImageName"]];

What is the difference between a UIImage and a UIImageView?

UIImage contains the data for an image. UIImageView is a custom view meant to display the UIImage .


2 Answers

Here we go.

This will store a UIImage into your documents directory of your iOS App. You won't need NSFileManager.

NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString * basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;  UIImage * imageToSave = [UIImage imageNamed:@"Icon.png"]; NSData * binaryImageData = UIImagePNGRepresentation(imageToSave);  [binaryImageData writeToFile:[basePath stringByAppendingPathComponent:@"myfile.png"] atomically:YES]; 

Edit: If you store images form the iOS Camera, you might look at how you can rotate the images to the right orientation. Look here in that case.

like image 112
Jonas Schnelli Avatar answered Oct 08 '22 19:10

Jonas Schnelli


To save it as a file, you'll either need to put it in a plist, or create a png/jpg representation of the image. You can save the UIImage data a little easier with NSCoding.

See this tutorial for more info: http://www.raywenderlich.com/1914/how-to-save-your-app-data-with-nscoding-and-nsfilemanager

like image 27
Matisse VerDuyn Avatar answered Oct 08 '22 18:10

Matisse VerDuyn