我终于开始将我们的一些图形库转换为使用CoreImage来渲染图像(例如混合两个图像),我已经让它工作了,但速度很慢(iOS5.01 iPhone 4S)。我认为CoreImage的承诺是硬件加速,这是我正在做的:
CIImage *intputBackgroundImage = [[CIImage alloc] initWithCGImage:backgroundImageRef];
CIImage *inputImage = [[CIImage alloc] initWithCGImage:inputImageRef];
CIFilter *multiply = [CIFilter filterWithName:@"CIMultiplyBlendMode"];
[multiply setDefaults];
[multiply setValue:intputBackgroundImage forKey:@"inputBackgroundImage"];
[multiply setValue:inputImage forKey:@"inputImage"];
CIImage *result = multiply.outputImage;
CIContext *ctx = [CIContext contextWithOptions:nil];
CGImageRef resultRef = [ctx createCGImage:result fromRect:CGRectMake(0, 0, imgFrame.size.width, imgFrame.size.height)];
UIImage *resultImage = [UIImage imageWithCGImage:resultRef];
CGImageRelease(resultRef);
它可以工作,我得到了一张将inputImage混合到backgroundImage中的图片,但是实际上这比我只使用CoreGraphics和CGContext()做的方式要慢。有没有办法检查这个工作是否不是在GPU上完成的,有没有原因导致不是呢?
谢谢。
glReadPixels()
调用以从GPU中获取信息也可以在10毫秒左右完成。这肯定比CPU更快地在GPU上完成,因此必须在其中某个地方存在一些隐藏的开销。也许在Core Image中的某个地方正在执行CPU上的格式转换(可能是从和到CGImageRef)。正如Peter所建议的那样,时间分析器可能会找出瓶颈在哪里。 - Brad Larson