EAGLView到UIImage颜色转换问题 [英] EAGLView to UIImage color conversion issue

查看:120
本文介绍了EAGLView到UIImage颜色转换问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个EAGLView(取自Apple的例子)我可以使用以下代码成功转换为UIImage:

I have an EAGLView (taken from Apple's examples) which I can successfully convert to a UIImage using this code:

- (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的代码:

And here is the code where I use this method to convert to a 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,图像的某些部分将比它想象的更亮。

The problem I am having is, sometimes the converted UIImage won't have the same colors as the EAGLView. This occurs if I apply a high saturation to the EAGLView, or high brigtness, or low contrast, and some other cases. For example, if I apply a high saturation to the EAGLView, and then convert to UIImage, some parts of the image will be brighter than what it's suppose to be.

所以我发现问题是伪装的EAGLView计时问题,类似于我之前的问题( EAGLView to UIImage计时问题)。

So I discovered that the problem was a EAGLView timing issue in disguise, similar to my previous question here (EAGLView to UIImage timing question).

推荐答案

对于任何仍在乎的人,请看下面的评论:

For anyone that still cares see my comment below:

汤米,我终于入侵了解决方案。这是另一个EAGLView计时问题(实际上只在Saturation期间出现),我能够使用你的performSelector修复它:afterDelay:0.0方法。 Thx

Tommy, I finally hacked my way into a solution. It was another EAGLView timing issue (which only showed up during Saturation actually), and I was able to fix it with your performSelector:afterDelay:0.0 approach. Thx

此外,我建议任何编写iOS 5.0的人都要研究Core Image和GLKView。它们基本上可以调整图像属性(就像我在这里做的那样)和EAGLView到UIImage转换类型的东西要简单得多。

Also, I would recommend anyone coding for iOS 5.0 to look into Core Image and GLKView. They basically make your job of adjusting image properties (as I am doing here) and EAGLView to UIImage conversion type of stuff much simpler.

这篇关于EAGLView到UIImage颜色转换问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆