我正在开发图片过滤器应用。我使用以下代码创建了过滤器:
context = [CIContext contextWithOptions:nil];
beginImage = [[CIImage alloc] initWithImage:[UIImage imageNamed:@"biscus_small.png"]];
filter = [CIFilter filterWithName:@"CISepiaTone"
keysAndValues: kCIInputImageKey, beginImage,
@"inputIntensity", [NSNumber numberWithFloat:0.8], nil];
然后,当用户选择过滤器时,我只需在过滤器应用后获取CIImage并分配给UIImageView。由于某种原因,setImage之后的UIImageView显示为白色(无)。
CIImage *outputImage = [filter outputImage];
// CGImageRef cgimg =
// [context createCGImage:outputImage fromRect:[outputImage extent]];
UIImage *finalImage = [UIImage imageWithCIImage:outputImage];
[self.imageView setImage:finalImage];
如果我使用CIContext的createCGImage方法,那么它工作正常并使用过滤器显示图像,但使用createCGImage非常慢。
答案 0 :(得分:6)
CIImage的-createCGImage:fromRect:
之所以如此之慢,是因为这实际上触发了栅格位图的处理和输出。将输出CIImage分配给UIImage不会导致处理和输出图像。通过Core Image过滤图像时,您将无法避免此步骤。
正如我在this answer中所说,Core Image在iOS上的表现有点令人失望。如果我可以提出建议,我已经构建了an open source framework,它可以进行GPU加速图像处理,并且在我测试的几乎所有情况下都能胜过Core Image的性能。对于图像过滤,我比iPhone 4上的Core Image快4倍。
但是,在过滤时从UIImage到UIImage仍然有一些开销,所以如果你能够使用原始图像字节,我的原始数据输入和输出比处理UIImage要快得多。我将在此框架中添加一些直接的JPEG和PNG输入和输出,以提供比通过UIImage更快的路径。