从
CIImage文档中了解到:
虽然CIImage对象具有与之关联的图像数据,但它不是图像。您可以将CIImage对象视为图像“配方”。 CIImage对象具有生成图像所需的所有信息,但Core Image实际上直到被告知才会呈现图像。这种“懒惰评估”方法使Core Image能够尽可能高效地运行。
这意味着您应用于CIImage的任何过滤器(或更好的申请应用)实际上不会发生,直到呈现图像,而在您的情况下,这发生在创建CGImageRef时。这很可能是您经历减速的原因。
话虽如此,在CIImage上进行过滤通常是一个非常快的过程,因此根据图像大小,您应该告诉我们您的缓慢定义。
最后,为确保瓶颈真的在您认为的位置,您应该使用Instruments
对应用程序进行配置文件分析。
编辑
我刚刚尝试构建一个示例项目,该项目通过应用CIColorMap颜色滤镜从设备相机(960x540)中过滤图像流,在iPad3上获得了超过60fps(〜16ms)。
根据您的应用程序,通过重用CIContext、ColorMapFilter、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 {
[self.colorMapFilter setValue:ciFrame forKey:@"inputImage"];
CIImage *ciImageResult = [self.colorMapFilter valueForKey: @"outputImage"];
CGImageRef ref = [self.context createCGImage:ciImageResult fromRect:ciFrame.extent];
CGImageRelease(ref);
}