首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >为什么iOS 6.0中的这段代码中的glReadPixels()会失败?

为什么iOS 6.0中的这段代码中的glReadPixels()会失败?
EN

Stack Overflow用户
提问于 2012-09-21 18:02:34
回答 1查看 9.1K关注 0票数 18

下面是我用来从OpenGL ES场景读取图像的代码:

代码语言:javascript
复制
-(UIImage *)getImage{

    GLint width;

    GLint height;

    glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &width);

    glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &height);


    NSLog(@"%d %d",width,height);

    NSInteger myDataLength = width * height * 4;

    // allocate array and read pixels into it.
    GLubyte *buffer = (GLubyte *) malloc(myDataLength);
    glReadPixels(0, 0, width, height, 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 < height; y++)
        {
        for(int x = 0; x < width * 4; x++)
            {
            buffer2[((height - 1) - y) * width * 4 + x] = buffer[y * 4 * width + 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 * width;
    CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
    CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
    CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;

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

    // then make the uiimage from that
    UIImage *myImage = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);
    CGDataProviderRelease(provider);
    CGColorSpaceRelease(colorSpaceRef);
    free(buffer);
    free(buffer2);
    return myImage;

}

这在iOS 5.x和更低版本中有效,但在iOS 6.0上,它现在返回黑色图像。为什么iOS 6.0上的glReadPixels()失败?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-09-25 01:29:18

代码语言:javascript
复制
CAEAGLLayer *eaglLayer = (CAEAGLLayer *) self.layer;
eaglLayer.drawableProperties = @{
    kEAGLDrawablePropertyRetainedBacking: [NSNumber numberWithBool:YES],
    kEAGLDrawablePropertyColorFormat: kEAGLColorFormatRGBA8
};

设置

代码语言:javascript
复制
kEAGLDrawablePropertyRetainedBacking = YES

(我不知道为什么这个技巧很好../)

票数 25
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12528365

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档