glReadPixels无法在iPhone上运行

时间:2012-11-06 15:42:50

标签: iphone objective-c c opengl-es glreadpixels

我正试图在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什么也没做。我故意将数组memset到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 );

有没有人有什么想法?它让我很生气:(

1 个答案:

答案 0 :(得分:1)

原来是iOS6对设置kEAGLDrawablePropertyRetainedBacking更加挑剔......

eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
                                    [NSNumber numberWithBool:YES],
                                    kEAGLDrawablePropertyRetainedBacking,
                                    kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat,
                                    nil];

请参阅here