In my project I am trying to get the average color of a series of UIImage
objects. So I implemented a category UIImage(AverageColor)
to calculate the average color.
Here's how I do it with the GPUImage
library:
- (void)averageColor:(void (^)(UIColor *))handler {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
GPUImagePicture *picture = [[GPUImagePicture alloc] initWithImage:self];
GPUImageAverageColor *averageColorFilter = [GPUImageAverageColor new];
[averageColorFilter setColorAverageProcessingFinishedBlock:^(CGFloat r, CGFloat g, CGFloat b, CGFloat a, CMTime frameTime) {
UIColor *color = [UIColor colorWithRed:r green:g blue:b alpha:a];
dispatch_async(dispatch_get_main_queue(), ^{
handler(color);
});
}];
[picture addTarget:averageColorFilter];
[picture useNextFrameForImageCapture];
[picture processImage];
});
}
I've also tried the approach (pure CPU one I think?) in this answer. Then I test these two methods with the same UIImage
and log out the time used in each methods. And here's the result:
cpu time: 0.102402
gpu time: 0.414044
I am surprised that the CPU one runs much faster. So what's the problem here? Am I using GPUImage
in a wrong way?
Edit:
The above result was got from iOS simulator. When testing on real device (iPhone6s) the difference is even greater:
cpu time: 0.019034
gpu time: 0.137635