glReadPixels returns iPhone 6 的图像不正确,但 iPad 和 iPhone 5 的图像正常

glReadPixels returns incorrect image for iPhone 6, but works ok for iPad and iPhone 5

我正在使用以下代码从 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];

有人可以阐明这个疯狂的魔法吗?

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

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

UPD:我的问题已通过以下代码解决:

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

int width = viewport[2];
int height = viewport[3];