Created
June 21, 2013 23:14
-
-
Save chinmaygarde/5835009 to your computer and use it in GitHub Desktop.
glReadPixels to Image on Desktop. Debugging Only
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
// Add ApplicationServices.h on Mac | |
#import <ImageIO/ImageIO.h> | |
#import <MobileCoreServices/MobileCoreServices.h> | |
CGSize sz = self.renderNode.size; | |
void *data = calloc(sz.width * sz.height * 4, sizeof(char)); | |
glReadPixels(0, 0, sz.width, sz.height, GL_RGBA, GL_UNSIGNED_BYTE, data); | |
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); | |
CGContextRef context = CGBitmapContextCreate(data, sz.width, sz.height, 8, sz.width * 4, colorSpace, kCGImageAlphaPremultipliedLast); | |
CGColorSpaceRelease(colorSpace); | |
CGImageRef image = CGBitmapContextCreateImage(context); | |
CGContextRelease(context); | |
CFURLRef url = (CFURLRef)[NSURL fileURLWithPath:@"/Users/Buzzy/Desktop/ReadPixels.png"]; | |
CGImageDestinationRef destination = CGImageDestinationCreateWithURL(url, kUTTypePNG, 1, NULL); | |
CGImageDestinationAddImage(destination, image, nil); | |
if (!CGImageDestinationFinalize(destination)) { | |
NSAssert(NO, @"Welp"); | |
} | |
CGImageRelease(image); | |
CFRelease(destination); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment