问题
I'm looking for way to grab the content of my opengl (as UIImage) and then save it into a file. I'm now giving glReadPixels a try though I'm not sure I'm doing the right thing as of what kind of malloc I should be doing. I gather that on OSX it's GL_BGRA but on the iPhone that doesn't work...
回答1:
All OpenGL|ES complient GL-implementations have to support GL_RGBA as a parameter to glReadPixels.
If your OpenGL|Es supports the
GL_IMPLEMENTATION_COLOR_READ_FORMAT_OES
extension you can also query the native format. glReadPixels will understand this format as a parameter to glReadPixels and allows you to directly get the pixel-data in native format. This is usually a bit faster than GL_RGBA.
Take a look at the header-file or query the extension-support via glGetString.
回答2:
Get the data out of the OpenGL ES view:
-(UIImage *) snapshot {
NSInteger myDataLength = backingWidth * backingHeight * 4;
// allocate array and read pixels into it.
GLubyte *buffer = (GLubyte *) malloc(myDataLength);
glReadPixels(0, 0, backingWidth, backingHeight, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
// gl renders "upside down" so swap top to bottom into new array.
// there's gotta be a better way, but this works.
GLubyte *buffer2 = (GLubyte *) malloc(myDataLength);
for(int y = 0; y <backingHeight; y++)
{
for(int x = 0; x <backingWidth * 4; x++)
{
buffer2[((backingHeight - 1) - y) * backingWidth * 4 + x] = buffer[y * 4 * backingWidth + x];
}
}
free(buffer);
// make data provider with data.
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer2, myDataLength, NULL);
// prep the ingredients
int bitsPerComponent = 8;
int bitsPerPixel = 32;
int bytesPerRow = 4 * backingWidth;
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
// make the cgimage
CGImageRef imageRef = CGImageCreate(backingWidth, backingHeight, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);
// then make the uiimage from that
UIImage *myImage = [UIImage imageWithCGImage:imageRef];
return myImage;
}
Then to save the image:
-(void) saveImage:(UIImage *)image {}
// Create paths to output images
NSString *pngPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Test.png"];
NSString *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Test.jpg"];
// Write a UIImage to JPEG with minimum compression (best quality)
// The value 'image' must be a UIImage object
// The value '1.0' represents image compression quality as value from 0.0 to 1.0
[UIImageJPEGRepresentation(image, 1.0) writeToFile:jpgPath atomically:YES];
// Write image to PNG
[UIImagePNGRepresentation(image) writeToFile:pngPath atomically:YES];
}
回答3:
Look up "Technical Q&A QA1704 OpenGL ES View Snapshot", where Apple gives a worked example, including Nils' "better way" to unflip the image. It gets you to your UIImage, and from there follow Nils for a means of saving as a file.
来源:https://stackoverflow.com/questions/314254/how-do-i-grab-an-image-from-my-eagllayer