用于多次采样的OpenGL ES快照,在iOS中提供奇怪的颜色

时间:2012-06-01 07:20:05

标签: ios opengl-es snapshot

当尝试将opengl视图快照作为UIImage进行多次采样时,图像颜色不同。

当多采样关闭时,它是正确的。 这就是我拍摄快照的方式:

- (UIImage*)snapshot
{

    GLint backingWidth, backingHeight;

    backingWidth = framebufferWidth;
    backingHeight = framebufferHeight;

    NSInteger myDataLength = backingWidth * backingHeight * 4;

    GLubyte *buffer = (GLubyte *) malloc(myDataLength);
    glReadPixels(0, 0, backingWidth, backingHeight, GL_RGBA, GL_UNSIGNED_BYTE, buffer);

    // gl renders "upside down" so swap top to bottom into new array.
    GLubyte *buffer2 = (GLubyte *) malloc(myDataLength);

    for(int y = 0; y < backingHeight; y++) {
        for(int x = 0; x < backingWidth * 4; x++) {
            buffer2[y*4*backingWidth + x] = buffer[(backingHeight - y -1 ) * backingWidth * 4 + x];
        }
    }
    // make data provider with data.
    CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer2, myDataLength, myProviderReleaseData);

    // prep the ingredients
    int bitsPerComponent = 8;
    int bitsPerPixel = 32;
    int bytesPerRow = 4 * backingWidth;
    CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
    CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedLast ;
    CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;

    // make the cgimage
    CGImageRef imageRef = CGImageCreate(backingWidth, backingHeight, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);

    // then make the uiimage from that
    UIImage *image1 = [UIImage imageWithCGImage:imageRef];

    CGImageRelease(imageRef);
    CGColorSpaceRelease(colorSpaceRef);
    CGDataProviderRelease(provider);
    free(buffer);

    return image1;
}

以下是拍摄快照的结果:

opengl view

snapshot of the same view

第一个是我正在绘制的opengl视图,第二个是我为上述代码获取的图像的快照。

我没有使用GLKit框架。想知道为什么多重采样会弄乱快照。

1 个答案:

答案 0 :(得分:1)

在拍摄opengl视图的快照之前检查是否正在调用[EAGLContext:presentRenderBuffer]方法