问题描述
已经在这个问题上苦苦挣扎了很长一段时间,到目前为止还没有找到答案.基本上,我想要做的是捕捉我的 EAGLview 的内容,然后用它来将它与其他图像合并.无论如何,主要问题是,将我的 EAGLview 中的所有透明内容保存到相册或将其放入 UIImageView 时都会呈现不透明.让我和你分享一些代码,我在别处找到的:
have been struggling with this issue for quite some time now and couldn't find an answer so far. Basically, what I want to do, is capturing the content of my EAGLview and then use it to merge it with other images. Anyway, the mainproblem is, that everything transparent in my EAGLview renders opaque when saving it to the photoalbum or putting it into a UIImageView. Let me share some code with you, I found somewhere else:
- (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 值的图像.
As I already mentioned, this perfectly grabs the content of my EAGLview, but I can not get the image with its alpha values.
任何帮助表示赞赏.谢谢!
Any help appreciated. Thanks!
推荐答案
有两个地方我可以看出你可能会失去透明度:
Two places I can see that you might be losing your transparency:
- 绘制场景时:场景是否有透明背景?确保您对 (0,0,0,0) 而不是 (0,0,0,1) 执行 glClear.
- 当您绘制图像以将其翻转时:此处的默认背景颜色是什么?似乎它是一种不透明的黑色,您最终会得到场景的透明部分曾经所在的位置.
您可以通过在翻转之前保存图像来检查 #2 是否是您的问题,如果是,您可以通过直接翻转像素缓冲区中的内存而不是使用 Core Graphics 调用来避免翻转过程.
You could check if #2 is your problem by saving the image before you flip it over, and if it is, you could avoid the flipping over process by flipping the memory in your pixels buffer directly rather than using Core Graphics calls.
这篇关于在 iPhone 上使用 alpha 通道捕获 EAGLview 内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!