Created
August 16, 2012 07:33
-
-
Save nxax/3368089 to your computer and use it in GitHub Desktop.
Take screenshots on ios
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
// http://developer.apple.com/library/ios/#qa/qa1703/_index.html#//apple_ref/doc/uid/DTS40010193 | |
- (UIImage*)screenshot | |
{ | |
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(); | |
return image; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment