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

时间:2010-01-28 23:08:27

标签: 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;

1 个答案:

答案 0 :(得分:1)

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

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