我正在尝试将OpenGL缓冲区(当前显示在视图中)保存到设备的照片库中。下面的代码片段在模拟器上运行正常。但对于实际设备而言,它正在崩溃。我相信在创建从屏幕捕获的UIImage的方式可能存在问题。
有人可以帮我解决这个问题吗?我想要一个从glReadPixels数据生成的有效UIImage引用。
以下是相关的代码段(调用保存到照片库):
-(void)TakeImageBufferSnapshot:(CGSize)dimensions
{
NSLog(@"TakeSnapShot 1 : (%f, %f)", dimensions.width, dimensions.height);
NSInteger size = dimensions.width * dimensions.height * 4;
GLubyte *buffer = (GLubyte *) malloc(size);
glReadPixels(0, 0, dimensions.width, dimensions.height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
GLubyte *buffer2 = (GLubyte *) malloc(size);
int height = (int)dimensions.height - 1;
int width = (int)dimensions.width;
for(int y = 0; y < dimensions.height; y++)
{
for(int x = 0; x < dimensions.width * 4; x++)
{
buffer2[(height - 1 - y) * width * 4 + x] = buffer[y * 4 * width + x];
}
}
NSLog(@"TakeSnapShot 2");
// make data provider with data.
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer2, size, NULL);
if (buffer) free(buffer);
if (buffer2) free(buffer2);
// prep the ingredients
int bitsPerComponent = 8;
int bitsPerPixel = 32;
int bytesPerRow = 4 * self.view.bounds.size.width;
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
NSLog(@"TakeSnapShot 3");
// make the cgimage
g_savePhotoImageRef = CGImageCreate(dimensions.width, dimensions.height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);
NSLog(@"TakeSnapShot 4");
// then make the uiimage from that
self.savePhotoImage = [UIImage imageWithCGImage:g_savePhotoImageRef];
CGColorSpaceRelease(colorSpaceRef);
CGDataProviderRelease(provider);
}
-(void)SaveToPhotoAlbum
{
ALAuthorizationStatus status = [ALAssetsLibrary authorizationStatus];
NSLog(@"Authorization status: %d", status);
if (status == ALAuthorizationStatusAuthorized)
{
[self TakeImageBufferSnapshot:self.view.bounds.size];
// UPDATED - DO NOT proceed to save to album below.
// Instead, set the created image to a UIImageView IBOutlet.
// On the simulator this shows the screen/buffer captured image (as expected) -
// but on the device (ipad) this doesnt show anything and the app crashes.
self.testImageView.image = self.savePhotoImage;
return;
NSLog(@"Saving to photo album...");
UIImageWriteToSavedPhotosAlbum(self.savePhotoImage,
self,
@selector(photoAlbumImageSave:didFinishSavingWithError:contextInfo:),
nil);
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Access is denied"
message:@"Allow access to your Photos library to save this image."
delegate:nil
cancelButtonTitle:@"Close"
otherButtonTitles:nil, nil];
[alert show];
}
}
- (void)photoAlbumImageSave:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)context
{
self.savePhotoImage = nil;
CGImageRelease(g_savePhotoImageRef);
if (error)
{
NSLog(@"Error saving photo to albums: %@", error.description);
}
else
{
NSLog(@"Saved to albums!");
}
}
*更新* 我想我已经设法缩小了我的问题范围。我开始做试验&amp;错误,我在评论出代码行后运行应用程序(在设备上),以缩小范围。看起来我的 TakeImageBufferSnapshot 函数可能有问题,它接受屏幕缓冲区(使用glReadPixels)并创建CGImageRef。现在,当我尝试创建一个UIImage时(使用[UIImage imageWithCGImage:]方法,这似乎是应用程序崩溃的原因。如果我评论这一行似乎没有问题(除了事实,我没有UIImage参考)。
我基本上需要一个有效的UIImage引用,以便我可以将它保存到照片库(使用测试图像似乎工作得很好)。
答案 0 :(得分:2)
首先,我应该指出glReadPixels()
可能不会按照您的预期行事。如果在调用-presentRenderbuffer:
后尝试使用它从屏幕读取,则结果未定义。例如,在iOS 6.0+上,它会返回黑色图像。您需要在内容显示到屏幕之前使用glReadPixels()
(我的建议),或者为OpenGL ES上下文启用保留后备(这会产生不利的性能影响)。
其次,不需要两个缓冲区。您可以直接捕获到一个并使用它来创建CGImageRef。
对于您的核心问题,问题是您在CGImageRef / UIImage仍然依赖它时释放原始图像字节缓冲区。这会从你的UIImage下方拉出地毯,并导致你看到的图像损坏/崩溃。为了解决这个问题,您需要在CGDataProvider的重新分配时触发一个回调函数。这是我在GPUImage框架中执行此操作的方式:
rawImagePixels = (GLubyte *)malloc(totalBytesForImage);
glReadPixels(0, 0, (int)currentFBOSize.width, (int)currentFBOSize.height, GL_RGBA, GL_UNSIGNED_BYTE, rawImagePixels);
dataProvider = CGDataProviderCreateWithData(NULL, rawImagePixels, totalBytesForImage, dataProviderReleaseCallback);
cgImageFromBytes = CGImageCreate((int)currentFBOSize.width, (int)currentFBOSize.height, 8, 32, 4 * (int)currentFBOSize.width, defaultRGBColorSpace, kCGBitmapByteOrderDefault | kCGImageAlphaLast, dataProvider, NULL, NO, kCGRenderingIntentDefault);
CGDataProviderRelease(dataProvider);
回调函数采用以下形式:
void dataProviderReleaseCallback (void *info, const void *data, size_t size)
{
free((void *)data);
}
仅当包含CGImageRef(以及扩展名CGDataProvider)的UIImage被释放时,才会调用此函数。在此之前,包含图像字节的缓冲区仍然存在。
作为一个功能性示例,您可以在GPUImage中检查我是如何做到这一点的。看一下GPUImageFilter类,了解如何从OpenGL ES框架中提取图像,包括使用纹理缓存而不是glReadPixels()
的更快方法。
答案 1 :(得分:1)
嗯 - 从我的经验来看,你现在不能只抓住缓冲区中的像素 你需要重新建立正确的上下文,在最终发布上下文之前绘制并抓住那些
=&GT;这主要适用于设备和特别是ios6
EAGLContext* previousContext = [EAGLContext currentContext];
[EAGLContext setCurrentContext: self.context];
[self fillBuffer:sender];
//GRAB the pixels here
[EAGLContext setCurrentContext:previousContext];
或者(这就是我怎么做)创建一个新的FrameBuffer,填充THAT并从THERE中抓取像素
GLuint rttFramebuffer;
glGenFramebuffers(1, &rttFramebuffer);
glBindFramebuffer(GL_FRAMEBUFFER, rttFramebuffer);
[self fillBuffer:self.displayLink];
size_t size = viewportHeight * viewportWidth * 4;
GLubyte *pixels = malloc(size*sizeof(GLubyte));
glPixelStorei(GL_PACK_ALIGNMENT, 4);
glReadPixels(0, 0, viewportWidth, viewportHeight, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
// Restore the original framebuffer binding
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
glDeleteFramebuffers(1, &rttFramebuffer);
size_t bitsPerComponent = 8;
size_t bitsPerPixel = 32;
size_t bytesPerRow = viewportWidth * bitsPerPixel / bitsPerComponent;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = kCGBitmapByteOrder32Big | kCGImageAlphaPremultipliedLast;
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, pixels, size, ImageProviderReleaseData);
CGImageRef cgImage = CGImageCreate(viewportWidth, viewportHeight, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpace, bitmapInfo, provider, NULL, true, kCGRenderingIntentDefault);
CGDataProviderRelease(provider);
UIImage *image = [UIImage imageWithCGImage:cgImage scale:self.contentScaleFactor orientation:UIImageOrientationDownMirrored];
CGImageRelease(cgImage);
CGColorSpaceRelease(colorSpace);
编辑:删除了对presentBuffer的调用