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]];
I want use souce
please
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
No.
It is very easy.
method easily have been resolved.
Have a nice day
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
Hey Steve,
Yes you are free to use it, totally!. It’s nice to know the tutorial helps you
Cheers,
Guerrix
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
Can we create folder in Documents folder?
I want to save image like folder1/image/img1.png
Thanks
Hi there, I think this could help you http://goo.gl/Xo86R
thanks,
It like we make recursion. add with the name that we want.
Thanks again for link.
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.