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]];
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.
very very thanks