【问题标题】:Creating UIImage with per-pixel alpha from GL buffer从 GL 缓冲区创建具有每像素 alpha 的 UIImage
【发布时间】:2010-01-28 23:08:27
【问题描述】:

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

【问题讨论】:

    标签: iphone opengl uiimage alpha


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2014-12-28
      • 1970-01-01
      • 1970-01-01
      • 2014-03-23
      • 2019-02-08
      • 2017-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多