Skip to content

Instantly share code, notes, and snippets.

@kosso
Created October 11, 2010 22:47
Show Gist options
  • Save kosso/621367 to your computer and use it in GitHub Desktop.
Save kosso/621367 to your computer and use it in GitHub Desktop.
// Kosso imageAsCropped
// added to TiBlob.m
// requires 4 arguments : x, y, width, height
- (id)imageAsCropped:(id)args
{
[self ensureImageLoaded];
if (image!=nil)
{
ENSURE_ARG_COUNT(args,4);
NSUInteger x = [TiUtils intValue:[args objectAtIndex:0]];
NSUInteger y = [TiUtils intValue:[args objectAtIndex:1]];
NSUInteger width = [TiUtils intValue:[args objectAtIndex:2]];
NSUInteger height = [TiUtils intValue:[args objectAtIndex:3]];
return [[[TiBlob alloc] initWithImage:[UIImageResize croppedImage:CGRectMake(x, y, width, height)
image:image]]
autorelease];
}
return nil;
}
// end Kosso imageAsCropped
// Kosso screenshot
// replacement of takeScreenshot method in MediaModule.m
//
-(void)takeScreenshot:(id)arg
{
ENSURE_UI_THREAD(takeScreenshot,arg);
ENSURE_SINGLE_ARG(arg,KrollCallback);
// Create a graphics context with the target size
// On iOS 4 and later, use UIGraphicsBeginImageContextWithOptions to take the scale into consideration
// On iOS prior to 4, fall back to use UIGraphicsBeginImageContext
float systemVersion = [[[UIDevice currentDevice] systemVersion] floatValue];
CGSize imageSize = [[UIScreen mainScreen] bounds].size;
// this wasn't working properly in different iOS versions so just check for >= 4.0
//if (NULL != UIGraphicsBeginImageContextWithOptions )
if (systemVersion >= 4.0f)
UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
else
UIGraphicsBeginImageContext(imageSize);
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();
TiBlob *blob = [[[TiBlob alloc] initWithImage:image] autorelease];
NSDictionary *event = [NSDictionary dictionaryWithObject:blob forKey:@"media"];
[self _fireEventToListener:@"screenshot" withObject:event listener:arg thisObject:nil];
}
// end Kosso screenshot
@joseandro
Copy link

Hi kosso, when using your module (or the most recent included in the SDK API) cropping big portrait images I get the result rotated 90 degrees to the left. I've been struggling to find a solution for that but I had not success so far, do you have any idea why this is happening?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment