Created
March 13, 2011 18:12
-
-
Save brianensorapps/868299 to your computer and use it in GitHub Desktop.
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
#import <Foundation/Foundation.h> | |
@interface SPDisplayObject (Screenshot) | |
- (UIImage *)screenshot; | |
@end |
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
#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