Skip to content

Instantly share code, notes, and snippets.

@rsaunders100
Created January 31, 2012 14:11
Show Gist options
  • Save rsaunders100/1710684 to your computer and use it in GitHub Desktop.
Save rsaunders100/1710684 to your computer and use it in GitHub Desktop.
(iOS) Take image snapshots of the current state of a view.
@interface UIView (UIView_Snapshots)
// Take image snapshots of the current state of a view.
// Use the background color of the superview if you have rounded corners
// If background color is nil the background will be treated as transparent
- (UIImage*) imageOfViewUsingBackgroundColor:(UIColor*)backgroundColorOrNil;
// Will save to the documents directory as XXXX.png
- (void) saveImageOfViewToPNGNamed:(NSString*)fileNameMinusExtension usingBackgroundColor:(UIColor*)backgroundColorOrNil;
@end
@implementation UIView (UIView_Snapshots)
- (UIImage*) imageOfViewUsingBackgroundColor:(UIColor*)backgroundColorOrNil
{
// Opaque is faster
BOOL opaqueCanvas = (backgroundColorOrNil) || (self.opaque && self.layer.cornerRadius == 0);
// Creeate a canvas
UIGraphicsBeginImageContextWithOptions(self.bounds.size, opaqueCanvas, 0.0);
// If we have a background collor begin by fillling the canvas
if (backgroundColorOrNil)
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, backgroundColorOrNil.CGColor);
CGContextFillRect(context, self.bounds);
}
// Render the view on the canvas
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
// Return the image repsrenstation of the canvas
UIImage* img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
- (void) saveImageOfViewToPNGNamed:(NSString*)fileNameMinusExtension usingBackgroundColor:(UIColor*)backgroundColorOrNil
{
// Create the path
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsDirectory = [paths objectAtIndex:0];
NSString* destPath = [documentsDirectory stringByAppendingPathComponent:fileNameMinusExtension];
destPath = [destPath stringByAppendingFormat:@".png"];
// Create the image
UIImage* image = [self imageOfViewUsingBackgroundColor:backgroundColorOrNil];
// Save as a PNG
[UIImagePNGRepresentation(image) writeToFile:destPath atomically:YES];
// Write out the contents of home directory to console
NSError *error;
NSFileManager *fileMgr = [NSFileManager defaultManager];
NSLog(@"Documents directory: %@", [fileMgr contentsOfDirectoryAtPath:documentsDirectory error:&error]);
}
@end
@interface UIView (UIView_Snapshots)
// Take image snapshots of the current state of a view.
// Use the background color of the superview if you have rounded corners
// If background color is nil the background will be treated as transparent
- (UIImage*) imageOfViewUsingBackgroundColor:(UIColor*)backgroundColorOrNil;
// Will save to the documents directory as XXXX.png
- (void) saveImageOfViewToPNGNamed:(NSString*)fileNameMinusExtension usingBackgroundColor:(UIColor*)backgroundColorOrNil;
@end
@implementation UIView (UIView_Snapshots)
- (UIImage*) imageOfViewUsingBackgroundColor:(UIColor*)backgroundColorOrNil
{
// Opaque is faster
BOOL opaqueCanvas = (backgroundColorOrNil) || (self.opaque && self.layer.cornerRadius == 0);
// Creeate a canvas
UIGraphicsBeginImageContextWithOptions(self.bounds.size, opaqueCanvas, 0.0);
// If we have a background collor begin by fillling the canvas
if (backgroundColorOrNil)
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, backgroundColorOrNil.CGColor);
CGContextFillRect(context, self.bounds);
}
// Render the view on the canvas
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
// Return the image repsrenstation of the canvas
UIImage* img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
- (void) saveImageOfViewToPNGNamed:(NSString*)fileNameMinusExtension usingBackgroundColor:(UIColor*)backgroundColorOrNil
{
// Create the path
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsDirectory = [paths objectAtIndex:0];
NSString* destPath = [documentsDirectory stringByAppendingPathComponent:fileNameMinusExtension];
destPath = [destPath stringByAppendingFormat:@".png"];
// Create the image
UIImage* image = [self imageOfViewUsingBackgroundColor:backgroundColorOrNil];
// Save as a PNG
[UIImagePNGRepresentation(image) writeToFile:destPath atomically:YES];
// Write out the contents of home directory to console (Optional test)
//NSError *error;
//NSFileManager *fileMgr = [NSFileManager defaultManager];
//NSLog(@"Documents directory: %@", [fileMgr contentsOfDirectoryAtPath:documentsDirectory error:&error]);
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment