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
@kosso
Copy link
Author

kosso commented Oct 11, 2010

Example use in a Titanium app to create a cropped image of a screenshot:

Titanium.Media.takeScreenshot(function(event){
    // get the image blob of the screenshot
    var screenShot = event.media;
    // I want to save it, so lets create a file
    var shotFile = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory + '/screenshot.png');
    // I only want one of these ever, so delete any old one
    if(shotFile.exists()){
        shotFile.deleteFile();
    }
    // perform imageAsCropped on the blob with arguments:  x, y, width, height (where x,y is from the top left corner) 
    var cropped = screenShot.imageAsCropped(10,10,200,200);
    // save the new file
    shotFile.write(cropped);
});

@pud
Copy link

pud commented Jan 29, 2011

n00b question - how do I install this patch?

@pud
Copy link

pud commented Jan 29, 2011

@BenRoe
Copy link

BenRoe commented Jul 12, 2011

This Module dosn't work with Titanium 1.7.1. Can someone fix it or is there another possibility to crop images?

@kosso
Copy link
Author

kosso commented Jul 12, 2011

You shouldn't need it now. It's actually in the SDK API. But used slightly differently now, with a dictionary of params.

@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