从CIImage 文档:
尽管 CIImage 对象具有与之关联的图像数据,但它不是图像。您可以将 CIImage 对象视为图像“配方”。CIImage 对象具有生成图像所需的所有信息,但 Core Image 不会真正渲染图像,直到被告知这样做。这种“惰性评估”方法允许 Core Image 尽可能高效地运行。
这意味着在您渲染图像之前,您可能已应用(或更好地要求应用)的任何过滤CIImage
都不会实际发生,在您的情况下,当您创建CGImageRef
. 这可能是您遇到减速的原因。
也就是说,过滤CIImage
通常是一个非常快的过程,因此根据您的图像大小,您应该给我们您对慢的定义。
最后,为了确保瓶颈确实在您认为的位置,您应该使用 Instruments 来分析您的应用程序。
编辑
我刚刚尝试构建一个示例项目,通过应用CIColorMap
颜色过滤器过滤来自 iPad3 上的设备相机 (960x540) 的图像流,我的速度超过 60fps (~16ms)。
根据您的应用程序,您将通过重用CIContext
、 the ColorMapFilter
、 the inputGradientImage
(如果它不随时间变化)并inputImage
在每次迭代时仅更新 来获得更好的性能。
例如,您将调用prepareFrameFiltering
一次,然后applyFilterToFrame:
在要处理的每一帧上重复调用。
@property (nonatomic, strong) CIContext *context;
@property (nonatomic, strong) CIFilter *colorMapFilter;
- (void)prepareFrameFiltering {
self.context = [CIContext contextWithOptions:nil];
CIImage *colorMap = [CIImage imageWithCGImage:[UIImage imageNamed:@"gradient.jpg"].CGImage];
self.colorMapFilter = [CIFilter filterWithName:@"CIColorMap"];
[self.colorMapFilter setValue:colorMap forKey:@"inputGradientImage"];
}
- (void)applyFilterToFrame:(CIImage *)ciFrame {
// filter
[self.colorMapFilter setValue:ciFrame forKey:@"inputImage"];
CIImage *ciImageResult = [self.colorMapFilter valueForKey: @"outputImage"];
CGImageRef ref = [self.context createCGImage:ciImageResult fromRect:ciFrame.extent];
// do whatever you need
CGImageRelease(ref);
}