我正在尝试在 iPhone 上截取 GLView 的屏幕截图。我写了以下代码:
[self setContext];
GLint backWidth, backHeight;
glGetRenderbufferParameterivOES( GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backWidth );
glGetRenderbufferParameterivOES( GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &backHeight );
int dataLength = backWidth * backHeight * 4;
uint32_t* pData = (uint32_t*)malloc( dataLength * sizeof( char ) );
memset( pData, 0xff, dataLength ); // This is here to confirm some writing occurs in glReadPixels
// Read pixel data from the framebuffer
//glPixelStorei( GL_PACK_ALIGNMENT, 4 );
glReadPixels( 0, 0, backWidth, backHeight, GL_RGBA, GL_UNSIGNED_BYTE, pData );
fprintf( stderr, "%d\n", glGetError() );
CGDataProviderRef cgDataProvider = CGDataProviderCreateWithData( NULL, pData, dataLength, DataProviderReleaseDataCallback );
CGColorSpaceRef cgColorSpace = CGColorSpaceCreateDeviceRGB();
CGImageRef cgImage = CGImageCreate( backWidth, backHeight, 8, 32, backWidth * 4, cgColorSpace, kCGBitmapByteOrder32Big | kCGImageAlphaPremultipliedLast,
cgDataProvider, NULL, true, kCGRenderingIntentDefault );
//NSData* pNSData = [[NSData alloc] initWithBytes: pData length: dataLength];
UIImage* pRetImage = [UIImage imageWithCGImage: cgImage scale: 1.0f orientation: UIImageOrientationDownMirrored];
CFRelease( cgDataProvider );
CFRelease( cgColorSpace );
CGImageRelease( cgImage );
//free( pData );
return pRetImage;
在模拟器中完美运行。不幸的是,当我尝试在 iPhone 4S glReadPixels 上运行它时,什么也没做。我故意将数组设置为 0xff 以查看是否可以查看它是否在做任何事情,而我将数组设置为什么也没关系 glReadPixels 什么都不做。它也不会报告任何错误。
除非 iPhone 为我进行设置,否则我不会使用多重采样缓冲区。
glGenRenderbuffers( 1, &mGlRenderBuffer );
glBindRenderbuffer( GL_RENDERBUFFER, mGlRenderBuffer );
[mGlContext renderbufferStorage: GL_RENDERBUFFER fromDrawable: mGlLayer];
GLuint frameBuffer;
glGenFramebuffers( 1, &frameBuffer );
glBindFramebuffer( GL_FRAMEBUFFER, frameBuffer );
glFramebufferRenderbuffer( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, mGlRenderBuffer );
有没有人知道发生了什么?它让我发疯:(