【发布时间】:2012-06-23 10:09:41
【问题描述】:
我正在使用以下代码截取GLView 中的像素的屏幕截图。问题是,它返回一个完全黑色的UIImage。这段代码在LineDrawer.m 中调用,这是GLView 代码的核心——所以它是从正确的.m 文件中调用的。如何保存实际的屏幕截图而不是黑色图像?
- (UIImage*) getGLScreenshot {
NSLog(@"1");
float scale = 0.0;
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
{
// scale value should be 1.0 on 3G and 3GS, and 2.0 on iPhone 4.
scale = [[UIScreen mainScreen] scale];
}
// these are swapped since the screen is rotatey
float h = 768 * scale;
float w = 924 * scale;
NSInteger myDataLength = w * h * 4;
// allocate array and read pixels into it.
GLubyte *buffer = (GLubyte *) malloc(myDataLength);
glReadPixels(0, 0, w, h, 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 <h; y++)
{
for(int x = 0; x <w * 4; x++)
{
buffer2[(((int)h-1) - y) * (int)w * 4 + x] = buffer[y * 4 * (int)w + 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 * w;
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
// make the cgimage
CGImageRef imageRef = CGImageCreate(w, h, 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);
}
【问题讨论】:
-
您需要释放两个缓冲区并释放
provider、colorSpaceRef和imageRef。您也可以通过在某处提供负的stride值来进行垂直翻转。 -
我该怎么做?抱歉,我对 OpenGL 不太熟悉,这是一个嵌入式库!我真的很感激一个例子:-)
-
我的评论不是您问题的答案,我指出您的内存泄漏。一般来说,您应该阅读 CoreFoundation 和 Objective-C 中的引用计数。在使用 malloc 时,您还需要注意 C 中的内存管理。
-
在运行上述捕获代码时,您的 OpenGL ES 上下文可能与当前线程没有关联。尝试在该方法的开头添加
[EAGLContext setCurrentContext:self.context];。此外,glReadPixels()只会捕获帧缓冲区中当前可见的内容,因此如果您在-drawRect:中为您的 GLKView 调用它,则该内容可能尚未呈现。我不确定使用 GLKView 强制执行此操作的正确方法,但glFinish()在glReadPixels()之前可能就足够了。 -
您是否尝试过没有在循环中翻转图像?您是否在控制台上输出了像素?它们真的是 0(黑色)吗?
标签: iphone objective-c ios xcode opengl-es