gpt4 book ai didi

iphone - EAGLView 到 UIImage 颜色转换问题

转载 作者:技术小花猫 更新时间:2023-10-29 10:40:23 26 4
gpt4 key购买 nike

我有一个 EAGLView(取自 Apple 的示例),我可以使用以下代码将其成功转换为 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 to UIImage timing question )。

最佳答案

对于仍然关心的人,请参阅下面我的评论:

Tommy,我终于找到了解决方案。这是另一个 EAGLView 计时问题(实际上仅在饱和期间出现),我能够使用您的 performSelector:afterDelay:0.0 方法修复它。谢谢

此外,我会推荐任何为 iOS 5.0 编码的人研究 Core Image 和 GLKView。它们基本上使您调整图像属性(就像我在这里所做的那样)和 EAGLView 到 UIImage 转换类型的工作变得更加简单。

关于iphone - EAGLView 到 UIImage 颜色转换问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7086468/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com