Como Guardar, Cargar o Borrar Imagenes de iPhone/iPad documents directory

Si en tu proyecto tienes la necesidad de cargar imagenes desde la web, sugiero que salves las imagenes dentro documents directory del tu iPhone/iPad , esto para optimizar la aplicacion y el performance.

Para guardar, cargar o eliminar una imagen del directorio de documentos de tu app, lo hacemos con 3 simples metodos.

Para Guardar una imagen

- (void)saveImage:(UIImage*)image:(NSString*)imageName {

NSData *imageData = UIImagePNGRepresentation(image);//convert image into .png format.
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");
}

Para Eliminar una imagen

- (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");

}

Para Cargar una imagen

- (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];

}

Ahora, para salvar cualquier imagen solo llamas al metodo algo asi;

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

o para cargar:

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

o para eliminar:

[self removeImage: @"UIImageName"];

Obtener una imagen desde una url:

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

13 thoughts on “Como Guardar, Cargar o Borrar Imagenes de 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.

Deja un comentario

Tu dirección de correo electrónico no será publicada. Los campos necesarios están marcados *

Puedes usar las siguientes etiquetas y atributos HTML: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>