EAGLView到UIImage颜色转换问题

我有一个EAGLView(取自苹果的例子),我可以成功地转换为UIImage使用此代码:

- (UIImage *)glToUIImage:(CGSize)size { NSInteger backingWidth = size.width; NSInteger backingHeight = size.height; NSInteger myDataLength = backingWidth * backingHeight * 4; // allocate array and read pixels into it. GLuint *buffer = (GLuint *) 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. for(int y = 0; y < backingHeight / 2; y++) { for(int x = 0; x < backingWidth; x++) { //Swap top and bottom bytes GLuint top = buffer[y * backingWidth + x]; GLuint bottom = buffer[(backingHeight - 1 - y) * backingWidth + x]; buffer[(backingHeight - 1 - y) * backingWidth + x] = top; buffer[y * backingWidth + x] = bottom; } } // make data provider with data. CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer, myDataLength, releaseScreenshotData); // prep the ingredients const int bitsPerComponent = 8; const int bitsPerPixel = 4 * bitsPerComponent; const int bytesPerRow = 4 * backingWidth; CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB(); CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault; CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault; // make the cgimage CGImageRef imageRef = CGImageCreate(backingWidth, backingHeight, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, YES, renderingIntent); CGColorSpaceRelease(colorSpaceRef); CGDataProviderRelease(provider); // then make the UIImage from that UIImage *myImage = [UIImage imageWithCGImage:imageRef]; CGImageRelease(imageRef); return myImage; } void releaseScreenshotData(void *info, const void *data, size_t size) { free((void *)data); }; 

这里是我使用此方法转换为UIImage的代码:

 EAGLView *newView = [[EAGLView alloc] initWithImage:photo.originalImage]; [newView reshapeFramebuffer]; [newView drawView:theSlider.tag value:theSlider.value]; //the two lines above are how EAGLViews in Apple's examples are modified photoItem *newPhoto = [[photoItem alloc] initWithImage:[self glToUIImage:photo.originalImage.size]]; 

我遇到的问题是,有时转换的UIImage将不会有与EAGLView相同的颜色。 如果我对EAGLView应用高饱和度,或者高亮度,低对比度和其他一些情况,就会发生这种情况。 例如,如果我将高饱和度应用于EAGLView,然后转换为UIImage,那么图像的某些部分会比想像的要亮。

所以我发现这个问题是一个EAGLView计时问题,类似于我之前的问题( EAGLView到UIImage计时问题 )。

对于任何仍然在意的人,请参阅下面的评论:

汤米,我终于黑了我的方式进入一个解决scheme。 这是另一个EAGLView计时问题(实际上在饱和期间才显示出来),我可以用你的performSelector:afterDelay:0.0方法修复它。 谢谢

另外,我会推荐任何编码iOS 5.0的人看看核心图像和GLKView。 他们基本上使你的工作调整图像属性(如我在这里做的)和EAGLView到UIImage转换types的东西要简单得多。