在其文档中,Apple说下面关于使用CoreImage实时过滤的内容:
如果您的应用支持实时图像处理,您应该从EAGL上下文创建CIContext对象,而不是使用contextWithOptions:并指定GPU。优点是渲染的图像保留在GPU上,永远不会被复制回CPU内存。首先,您需要创建EAGL上下文:
我能够使用以下实现在iOS6 / iPhone5中完成此任务:
self.eaglContext = [[[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2] autorelease];
self.ciContext = [[CIContext contextWithEAGLContext:_eaglContext options:[[[NSMutableDictionary alloc] initWithObjectsAndKeys:[NSNull null], kCIContextWorkingColorSpace, nil] autorelease]] autorelease];
self.filteredImage = [[CIImage imageWithCGImage:_image.CGImage] autorelease];
GLKView *glkView = [[GLKView alloc] initWithFrame:self.imageView.bounds context:self.eaglContext];
self.glkView = glkView;
[self.glkView bindDrawable];
[self.mainView addSubview:self.glkView];
[glkView release];
CGRect filterViewBounds = CGRectZero;
filterViewBounds.size.width = self.glkView.drawableWidth;
filterViewBounds.size.height = self.glkView.drawableHeight;
self.filterViewBounds = filterViewBounds;
[self.context drawImage:self.filteredImage inRect:self.filterViewBounds fromRect:[self.filteredImage extent]];
[self.glkView display];
然后按以下方式应用过滤器:
CIImage *filteredImage = [[CIImage alloc] initWithCGImage:self.image.CGImage options:nil];
for (Filter *filter in self.filters)
{
if ([filter apply])
{
[filter.filter setValue:filteredImage forKey:kCIInputImageKey];
[filteredImage release];
filteredImage = [[filter.filter outputImage] retain];
}
}
[self.glkView bindDrawable];
[self.ciContext drawImage:filteredImage inRect:self.filterViewBounds fromRect:[filteredImage extent]];
[self.glkView display];
然而,在iOS5 / iPhone4上,当我去应用过滤器时,我得到了:
CoreImage: EAGLContext framebuffer or renderbuffer incorrectly configured!
此后过滤确实有效,但是性能仍然非常糟糕,几乎与我将过滤后的图像保存回CPU相似。事实上,当我退出这个特定的视图控制器,然后尝试回到它,它崩溃。
任何人都可以提供任何帮助吗?