Created
November 11, 2015 14:58
-
-
Save rpavez/ddf55584675ffa9fdbf3 to your computer and use it in GitHub Desktop.
Titanium takeScreenshot code
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)takeScreenshot:(id)arg | |
{ | |
ENSURE_SINGLE_ARG(arg,KrollCallback); | |
ENSURE_UI_THREAD(takeScreenshot,arg); | |
// Create a graphics context with the target size | |
CGSize imageSize = [[UIScreen mainScreen] bounds].size; | |
UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0); | |
CGContextRef context = UIGraphicsGetCurrentContext(); | |
// Iterate over every window from back to front | |
for (UIWindow *window in [[UIApplication sharedApplication] windows]) | |
{ | |
if (![window respondsToSelector:@selector(screen)] || [window screen] == [UIScreen mainScreen]) | |
{ | |
// -renderInContext: renders in the coordinate space of the layer, | |
// so we must first apply the layer's geometry to the graphics context | |
CGContextSaveGState(context); | |
// Center the context around the window's anchor point | |
CGContextTranslateCTM(context, [window center].x, [window center].y); | |
// Apply the window's transform about the anchor point | |
CGContextConcatCTM(context, [window transform]); | |
// Offset by the portion of the bounds left of and above the anchor point | |
CGContextTranslateCTM(context, | |
-[window bounds].size.width * [[window layer] anchorPoint].x, | |
-[window bounds].size.height * [[window layer] anchorPoint].y); | |
// Render the layer hierarchy to the current context | |
[[window layer] renderInContext:context]; | |
// Restore the context | |
CGContextRestoreGState(context); | |
} | |
} | |
// Retrieve the screenshot image | |
UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); | |
UIGraphicsEndImageContext(); | |
if (![TiUtils isIOS8OrGreater]) { | |
UIInterfaceOrientation windowOrientation = [[UIApplication sharedApplication] statusBarOrientation]; | |
switch (windowOrientation) { | |
case UIInterfaceOrientationPortraitUpsideDown: | |
image = [UIImage imageWithCGImage:[image CGImage] scale:[image scale] orientation:UIImageOrientationDown]; | |
break; | |
case UIInterfaceOrientationLandscapeLeft: | |
image = [UIImage imageWithCGImage:[image CGImage] scale:[image scale] orientation:UIImageOrientationRight]; | |
break; | |
case UIInterfaceOrientationLandscapeRight: | |
image = [UIImage imageWithCGImage:[image CGImage] scale:[image scale] orientation:UIImageOrientationLeft]; | |
break; | |
default: | |
break; | |
} | |
} | |
TiBlob *blob = [[[TiBlob alloc] initWithImage:image] autorelease]; | |
NSDictionary *event = [NSDictionary dictionaryWithObject:blob forKey:@"media"]; | |
[self _fireEventToListener:@"screenshot" withObject:event listener:arg thisObject:nil]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment