Last active
August 29, 2015 14:22
-
-
Save Adnan1990/88a84e03960fe7a9a1bb to your computer and use it in GitHub Desktop.
Saving and retrieving images to or from document directory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//The following function saves UIImage in test.png file in the user Document folder: | |
- (void)saveImage: (UIImage*)image | |
{ | |
if (image != nil) | |
{ | |
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, | |
NSUserDomainMask, YES); | |
NSString *documentsDirectory = [paths objectAtIndex:0]; | |
NSString* path = [documentsDirectory stringByAppendingPathComponent: | |
[NSString stringWithString: @"test.png"] ]; | |
NSData* data = UIImagePNGRepresentation(image); | |
[data writeToFile:path atomically:YES]; | |
} | |
} | |
//The following function loads UIImage from the test.png file: | |
- (UIImage*)loadImage | |
{ | |
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, | |
NSUserDomainMask, YES); | |
NSString *documentsDirectory = [paths objectAtIndex:0]; | |
NSString* path = [documentsDirectory stringByAppendingPathComponent: | |
[NSString stringWithString: @"test.png"] ]; | |
UIImage* image = [UIImage imageWithContentsOfFile:path]; | |
return image; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment