Created
January 31, 2012 14:11
-
-
Save rsaunders100/1710684 to your computer and use it in GitHub Desktop.
(iOS) Take image snapshots of the current state of a view.
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
@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 |
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
@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