iphone - 使用 GL 缓冲区中的每像素 alpha 创建 UIImage

标签 iphone opengl uiimage alpha

我正在尝试获取我的 gl 缓冲区并将其转换为 UIImage,同时保留该 gl 缓冲区中的每像素 alpha。它似乎不起作用,因为我得到的结果是没有 alpha 的缓冲区。有人可以帮忙吗?我觉得我一定在某个地方错过了几个关键步骤。我真的很乐意就此提出任何建议。

基本上我是这样的:

//Read Pixels from OpenGL
glReadPixels(0, 0, miDrawBufferWidth, miDrawBufferHeight, GL_RGBA, GL_UNSIGNED_BYTE, buffer);

//Make data provider with data.
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer, len, NULL);

//Configure image
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGImageRef iref = CGImageCreate(miDrawBufferWidth, miDrawBufferHeight, 8, 32, (4 * miDrawBufferWidth), colorSpaceRef, kCGBitmapByteOrderDefault, provider, NULL, NO, kCGRenderingIntentDefault);

// use device's orientation's width/height to determine context dimensions (and consequently resulting image's dimensions)
uint32* pixels = (uint32 *) IQ_NEW(kHeapGfx, "snapshot_pixels") GLubyte[len];

// use kCGImageAlphaLast? :-/
CGContextRef context = CGBitmapContextCreate(pixels, iRotatedWidth, iRotatedHeight, 8, (4 * iRotatedWidth), CGImageGetColorSpace(iref), kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);


CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, miDrawBufferWidth, miDrawBufferHeight), iref);
UIImage *outputImage = [[UIImage alloc] initWithCGImage:CGBitmapContextCreateImage(context)];

//cleanup
CGDataProviderRelease(provider);
CGImageRelease(iref);
CGContextRelease(context);

return outputImage;

最佳答案

是的!幸运的是,显然有人在这里解决了这个确切的问题:http://www.iphonedevsdk.com/forum/iphone-sdk-development/23525-cgimagecreate-alpha.html

它归结为一个额外的 kCGImageAlphaLast 标志被传递到 CGImageCreate 中以合并 alpha(以及 kCGBitmapByteOrderDefault 标志)。 :)

关于iphone - 使用 GL 缓冲区中的每像素 alpha 创建 UIImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2158706/

相关文章:

iphone - 调整 UITextView 的大小

iphone - 获取iphone中tesseract Ocr中的文本位置

iphone - Symbolicate iPhone 应用程序崩溃

python - glBufferSubData 不起作用,不显示错误

c++ - 如何在 Windows 上安装 FTGL 库?

ios - [UIImageView CGImage] : unrecognized selector sent to instance 0x1783e5b00

iphone - XCode无法在设备上运行应用程序,说正在调试的程序没有运行

image - 我怎么知道图像是否为 "Premultiplied Alpha"?

ios - 如何以编程方式更改 UISlider 的缩略图大小

ios - 从 2 个 UIImages 和标签创建 UIImage