2

我正在尝试获取我的 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;
4

1 回答 1

1

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

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

于 2010-01-29T04:56:15.703 回答