Skip to content

Instantly share code, notes, and snippets.

@cokecoffe
Last active December 14, 2015 04:28
Show Gist options
  • Select an option

  • Save cokecoffe/5028073 to your computer and use it in GitHub Desktop.

Select an option

Save cokecoffe/5028073 to your computer and use it in GitHub Desktop.
Save and Load UIImage in Documents directory on iPhone
//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