Skip to content

Instantly share code, notes, and snippets.

@chinmaygarde
Created June 21, 2013 23:14
Show Gist options
  • Save chinmaygarde/5835009 to your computer and use it in GitHub Desktop.
Save chinmaygarde/5835009 to your computer and use it in GitHub Desktop.
glReadPixels to Image on Desktop. Debugging Only
// 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