22

How I can save UIImage to file with NSFileManager ?

Thank,

user1234096
  • 281
  • 1
  • 2
  • 6

2 Answers2

55

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.

Community
  • 1
  • 1
Jonas Schnelli
  • 9,965
  • 3
  • 48
  • 60
1

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

Matisse VerDuyn
  • 1,148
  • 15
  • 40
  • 1
    Hmm... why use NSCoding? Okay: maybe when you try to store complex object structures with UIImages in it. The coding process also eats CPU ticks. – Jonas Schnelli Apr 10 '12 at 18:30