这个问题困扰我很久了,目前还没有找到答案。基本上,我想做的是捕获 EAGLview 的内容,然后用它与其他图像合并。无论如何,主要问题是,当将我的 EAGLview 中的所有透明内容保存到相册或将其放入 UIImageView 中时,它都会呈现不透明。让我与您分享一些代码,我在其他地方找到的:
- (CGImageRef) glToUIImage {
unsigned char buffer[320*480*4];
glReadPixels(0,0,320,480,GL_RGBA,GL_UNSIGNED_BYTE,&buffer);
CGDataProviderRef ref = CGDataProviderCreateWithData(NULL, &buffer, 320*480*4, NULL);
CGImageRef iref = CGImageCreate(320,480,8,32,320*4,CGColorSpaceCreateDeviceRGB(),kCGBitmapByteOrderDefault,ref,NULL,true,kCGRenderingIntentDefault);
size_t width = CGImageGetWidth(iref);
size_t height = CGImageGetHeight(iref);
size_t length = width*height*4;
uint32_t *pixels = (uint32_t *)malloc(length);
CGContextRef context = CGBitmapContextCreate(pixels, width, height, 8, width*4, CGImageGetColorSpace(iref), kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGContextTranslateCTM(context, 0.0, height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextDrawImage(context, CGRectMake(0.0, 0.0, width, height), iref);
CGImageRef outputRef = CGBitmapContextCreateImage(context);
UIImage *outputImage = [UIImage imageWithCGImage:outputRef];
UIImageWriteToSavedPhotosAlbum(outputImage, nil, nil, nil);
return outputRef;
}
正如我已经提到的,这完美地捕获了我的 EAGLview 的内容,但我无法获取带有 alpha 值的图像。
任何帮助表示赞赏。谢谢!
最佳答案
我发现您可能会在两个地方失去透明度:
- 当您绘制场景时:您的场景是否有透明背景?确保您对类似 (0,0,0,0) 而不是 (0,0,0,1) 的内容执行 glClear。
- 当您绘制图像以将其翻转时:这里的默认背景颜色是什么?看起来它可能是不透明的黑色,您最终会得到场景中透明部分曾经所在的位置。
您可以通过在翻转图像之前保存图像来检查#2是否是您的问题,如果是,您可以通过直接翻转像素缓冲区中的内存而不是使用 Core Graphics 调用来避免翻转过程。
关于iphone - 在 iPhone 上使用 Alpha channel 捕获 EAGLview 内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/962390/