何时可以使用glReadPixels?
问题描述:
我想知道GLReadPixels
函数的用法./ 它是如何读取像素的? 它读取GLKView
像素或UIView
像素或主屏幕上的任何内容,它位于glreadFunction中提供的边界内。 或者它只能用于如果我们使用GLKView
??何时可以使用glReadPixels?
请澄清我的疑问。
答
它从当前的OpenGL(ES)帧缓冲读取像素。它不能用于读取UIView
中的像素,但它可以用于从GLKView
中读取数据,因为它由帧缓冲区支持(但是,只能在其活动帧缓冲区读取其数据时,它最有可能位于绘图时间)。但是,如果您想要的任何内容都是您的屏幕截图GLKView
,则可以使用其内置的snapshot
方法获取UIImage
及其内容。
答
您可以使用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);
}
感谢您的解释。 还有一件事,glreadpixel如何用来拍摄包含许多子视图(glkviews的观点)的glkview快照? – 2012-07-10 12:19:31
@KaranSehgal它不能。另外,GLKViews的UIView没有这样的东西。 – JustSid 2012-07-10 12:26:33
嘿男人感谢info.really欣赏它。 我不知道opengl,Ijust想要子视图的截图。但是renderInContext方法需要花费很多时间,因为我经常使用屏幕快照制作视频。所以我在某处读到使用opengl可以解决我的问题。所以,你对此有任何想法吗? – 2012-07-10 12:30:36