How to Save,Load or Remove Images in iPhone/iPad documents directory

If in your project you frequently need load images from the web, i suggest save them in the local documents directory in your iPhone or iPad to optimize your app and the performance. For Save, Load or Remove a image you can do it with three simple methods:

Saving an image

- (void)saveImage:(UIImage*)image:(NSString*)imageName {
//convert image into .png format.
NSData *imageData = UIImagePNGRepresentation(image);
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:
[NSString stringWithFormat:@"%@.png", imageName]];

[fileManager createFileAtPath:fullPath contents:imageData attributes:nil];
NSLog(@"image saved");
}

Removing an image

- (void)removeImage:(NSString*)fileName {

NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:
[NSString stringWithFormat:@"%@.png", fileName]];

[fileManager removeItemAtPath: fullPath error:NULL];
NSLog(@"image removed");

}

Load an image

- (UIImage*)loadImage:(NSString*)imageName {

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:
[NSString stringWithFormat:@"%@.png", imageName]];

return [UIImage imageWithContentsOfFile:fullPath];

}

Now, you can save any image doing something like this:

[self saveImage: theUIImage: @"UIImageName"];

or you can load it like this:

myUIImage = [self loadImage: @"UIImageName"];

or remove it like:

[self removeImage: @"UIImageName"];

Get image from a url:

NSString *urlName= @"http://theURL/image";
NSURL *url = [NSURL URLWithString:urlName];
UIImage *myImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];

13 thoughts on “How to Save,Load or Remove Images in iPhone/iPad documents directory

    • Sorry Park, but i don’t have a source sample(i mean in a project), but is quite easy implement those methods in your code. Just add the methods and call them just as indicated in this post .

      Cheers

  1. Good tutorial thank you!
    Question: I have a vewController that opens the camera and you can take a picture then it saves the image. In my secondViewController I need to load this image that was taken ans saved in my firstViewController. Can I use what you have done in the tutorial to accomplish saving the image from the camera then retrieving it in a different view?

    Thanks,
    Steve

  2. hello sir, thanks for ur article.. its really nice.. i tried out automation testing with automation tool for ur hello world example.. but i cant able to choose target and it is showing fail…

    • Are you using your the iPhone Simulator? If so, look for the target in the iPhone simulator Directory “/Users//Library/Application Support/iPhone Simulator/User/Applications”….

      Let me know if that work.

      Cheers

  3. Good tutorial thank you!
    Question: I have a viewController that opens the camera and it take a picture then it saves the image in documents directory.i tried to save an image using this code [self saveImage: theUIImage: @"UIImageName"]; i already include save image coding given in your tutorial.but its showing error in calling that method as”Extraneous ‘]’before;”

    Thanks

    • Hey Ipkm,

      You can add a few lines in the next delegate method for the UIImagePickerControllerDelegate.

      That would work, I tested it. Let’me know if works for you.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>