0

我正在使用以下代码从 OpenGL ES 场景中读取图像:

- (UIImage *)drawableToCGImage
{
CGRect myRect = self.bounds;
NSInteger myDataLength = myRect.size.width * myRect.size.height * 4;

glFinish();
glPixelStorei(GL_PACK_ALIGNMENT, 4);

int width = myRect.size.width;
int height = myRect.size.height;

GLubyte *buffer = (GLubyte *) malloc(myDataLength);
GLubyte *buffer2 = (GLubyte *) malloc(myDataLength);
glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer2);
for(int y1 = 0; y1 < height; y1++) {
    for(int x1 = 0; x1 < width * 4; x1++) {
        buffer[(height - 1 - y1) * width * 4 + x1] = buffer2[y1 * 4 * width + x1];
    }
}

free(buffer2);

CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer, myDataLength, NULL);
int bitsPerComponent = 8;
int bitsPerPixel = 32;
int bytesPerRow = 4 * myRect.size.width;
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
CGImageRef imageRef = CGImageCreate(myRect.size.width, myRect.size.height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);
CGColorSpaceRelease(colorSpaceRef);
CGDataProviderRelease(provider);
UIImage *image = [UIImage imageWithCGImage:imageRef];

CGImageRelease(imageRef);

return image;
}

它适用于 iPad 和旧 iPhone 版本,但我注意到在 iPhone 6(设备和模拟器)上它看起来像单色故障。会是什么呢?

另外,这是我的 CAEAGLLayer 属性代码:

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

有人能解释一下这个疯狂的魔法吗?

4

1 回答 1

0

感谢@MaticOblak,我已经解决了问题。

缓冲区填充不正确,因为矩形大小的浮点值未正确舍入(是的,仅适用于 iPhone 6 尺寸)。相反,应该使用整数值。

UPD:我的问题已通过以下代码修复:

GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport);

int width = viewport[2];
int height = viewport[3];
于 2016-02-24T12:46:36.463 回答