【发布时间】:2012-07-09 21:47:58
【问题描述】:
我想知道GLReadPixels函数的使用。/
它是如何读取像素的?
它是在读取GLKView 像素还是UIView 像素或主屏幕上在 glreadFunction 提供的范围内的任何内容。
或者只有在我们使用GLKView时才能使用它??
请澄清我的疑问。
【问题讨论】:
标签: iphone xcode ipad glreadpixels
我想知道GLReadPixels函数的使用。/
它是如何读取像素的?
它是在读取GLKView 像素还是UIView 像素或主屏幕上在 glreadFunction 提供的范围内的任何内容。
或者只有在我们使用GLKView时才能使用它??
请澄清我的疑问。
【问题讨论】:
标签: iphone xcode ipad glreadpixels
它从当前的 OpenGL (ES) 帧缓冲区中读取像素。它不能用于从UIView 读取像素,但它可以用于从GLKView 读取像素,因为它由帧缓冲区支持(但是,您只能在它最活跃的帧缓冲区时读取它的数据可能是在绘图时)。但是,如果您想要的只是GLKView 的屏幕截图,您可以使用其内置的snapshot 方法来获取带有其内容的UIImage。
【讨论】:
GLKView 包含子视图,您根本无法通过 OpenGL 捕获它们。在这种情况下,您必须使用另一种方法,例如 renderInContext:,顺便说一句,这是可用的最快方法。
您可以使用 glreadPixels 读取背景屏幕。这是要执行的代码。
- (UIImage*) getGLScreenshot {
NSInteger myDataLength = 320 * 480 * 4;
// allocate array and read pixels into it.
GLubyte *buffer = (GLubyte *) malloc(myDataLength);
glReadPixels(0, 0, 320, 480, 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 <480; y++)
{
for(int x = 0; x <320 * 4; x++)
{
buffer2[(479 - y) * 320 * 4 + x] = buffer[y * 4 * 320 + x];
}
}
// make data provider with data.
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer2, myDataLength, NULL);
// prep the ingredients
int bitsPerComponent = 8;
int bitsPerPixel = 32;
int bytesPerRow = 4 * 320;
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
// make the cgimage
CGImageRef imageRef = CGImageCreate(320, 480, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);
// then make the uiimage from that
UIImage *myImage = [UIImage imageWithCGImage:imageRef];
return myImage;
}
- (void)saveGLScreenshotToPhotosAlbum {
UIImageWriteToSavedPhotosAlbum([self getGLScreenshot], nil, nil, nil);
}
【讨论】: