Reference link :
http://www.friendlydeveloper.com/2010/02/using-nsfilemanager-to-save-an-image-to-or-loadremove-an-image-from-documents-directory-coding/
special thanks for "
friendlydeveloper"
NSFileManager offers a convenient way to write images to and load them from the documents directory.
If you’re frequently doing that in your project, I suggest to wrap up NSFileManager support in three simple methods:
03 | - (void)saveImage:(UIImage*)image:(NSString*)imageName { |
05 | NSData *imageData = UIImagePNGRepresentation(image); |
07 | NSFileManager *fileManager = [NSFileManager defaultManager]; |
09 | NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); |
11 | NSString *documentsDirectory = [paths objectAtIndex:0]; |
13 | NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png", imageName]]; |
15 | [fileManager createFileAtPath:fullPath contents:imageData attributes:nil]; |
23 | - (void)removeImage:(NSString*)fileName { |
25 | NSFileManager *fileManager = [NSFileManager defaultManager]; |
27 | NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); |
29 | NSString *documentsDirectory = [paths objectAtIndex:0]; |
31 | NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png", fileName]]; |
33 | [fileManager removeItemAtPath: fullPath error:NULL]; |
35 | NSLog(@"image removed"); |
41 | - (UIImage*)loadImage:(NSString*)imageName { |
43 | NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); |
45 | NSString *documentsDirectory = [paths objectAtIndex:0]; |
47 | NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png", imageName]]; |
49 | return [UIImage imageWithContentsOfFile:fullPath]; |
Now, you can easily save an image like:
1 | [self saveImage: myUIImage: @"myUIImageName"]; |
or load it like:
1 | myUIImage = [self loadImage: @"myUIImageName"]; |
or remove it like:
1 | [self removeImage: @"myUIImageName"]; |