Created
February 18, 2013 15:26
-
-
Save daveverwer/4978196 to your computer and use it in GitHub Desktop.
This file contains 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
- (void)saveScreenshotToDocumentsDirectory | |
{ | |
CGSize imageSize = [[UIScreen mainScreen] bounds].size; | |
if (NULL != UIGraphicsBeginImageContextWithOptions) | |
UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0); | |
else UIGraphicsBeginImageContext(imageSize); | |
CGContextRef context = UIGraphicsGetCurrentContext(); | |
for (UIWindow *window in [[UIApplication sharedApplication] windows]) { | |
if (![window respondsToSelector:@selector(screen)] || [window screen] == [UIScreen mainScreen]) { | |
CGContextSaveGState(context); | |
CGContextTranslateCTM(context, [window center].x, [window center].y); | |
CGContextConcatCTM(context, [window transform]); | |
CGContextTranslateCTM(context, -[window bounds].size.width * [[window layer] anchorPoint].x, -[window bounds].size.height * [[window layer] anchorPoint].y); | |
[[window layer] renderInContext:context]; | |
CGContextRestoreGState(context); | |
} | |
} | |
UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); | |
UIGraphicsEndImageContext(); | |
NSData *imageData = UIImagePNGRepresentation(image); | |
NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; | |
NSString *imagePath = [documentsPath stringByAppendingPathComponent:@"Screenshot.png"]; | |
[[NSFileManager defaultManager] createFileAtPath:imagePath contents:imageData attributes:nil]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment