Skip to content

Instantly share code, notes, and snippets.

@brianensorapps
Created March 13, 2011 18:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brianensorapps/868299 to your computer and use it in GitHub Desktop.
Save brianensorapps/868299 to your computer and use it in GitHub Desktop.
#import <Foundation/Foundation.h>
@interface SPDisplayObject (Screenshot)
- (UIImage *)screenshot;
@end
#import "SPDisplayObject+Screenshot.h"
@implementation SPDisplayObject (Screenshot)
- (UIImage *)screenshot {
int bufferLenght = (self.width*(self.height+30)*4);
int myWidth = self.width;
int myHeight = self.height;
int myY = self.stage.height-self.y-self.height;
int myX = self.x;
unsigned char buffer[bufferLenght];
glReadPixels(myX, myY, myWidth, myHeight, GL_RGBA, GL_UNSIGNED_BYTE, &buffer);
CGDataProviderRef ref = CGDataProviderCreateWithData(NULL, &buffer, bufferLenght, NULL);
CGImageRef iref = CGImageCreate(myWidth,myHeight,8,32,myWidth*4,CGColorSpaceCreateDeviceRGB(),
kCGBitmapByteOrderDefault,ref,NULL, true, kCGRenderingIntentDefault);
uint32_t* pixels = (uint32_t *)malloc(bufferLenght);
CGContextRef context = CGBitmapContextCreate(pixels, myWidth, myHeight, 8, myWidth*4, CGImageGetColorSpace(iref),
kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder32Big);
CGContextTranslateCTM(context, 0.0, myHeight);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextDrawImage(context, CGRectMake(0.0, 0.0, myWidth, myHeight), iref);
CGImageRef outputRef = CGBitmapContextCreateImage(context);
UIImage *image = [UIImage imageWithCGImage:outputRef];
free(pixels);
return image;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment