UITableViewCell中的图像模糊时的性能问题 [英] Performance Issue while blurring image in UITableViewCell
问题描述
我的自定义UITableViewCell
中有一个UIImageView
.包含的图像应模糊.我知道UIVisualEffectsView
,但是首先,这在iOS8
之前是不可用的,其次,对于我的用例而言,模糊有些沉重.
I got an UIImageView
in my custom UITableViewCell
. The included image should be blurred. I am aware of UIVisualEffectsView
but first of all this is not available prior iOS8
and second the blur is a bit to heavy for my use case.
这就是为什么我想出这个解决方案的原因:
Thats why I came up with this solution:
cellForRowAtIndexPath示例:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"showCell";
DEShowCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
[tableView registerNib:[UINib nibWithNibName:@"DEShowCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:cellIdentifier];
cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
}
[cell setBackgroundImageWithBlur:[UIImage imageNamed:@"sampleBanner"]];
return cell;
}
我的自定义单元格:
-(void)setBackgroundImageWithBlur:(UIImage *)image {
[self.backgroundImageView setImage:[self blurWithCoreImage:image]];
}
- (UIImage *)blurWithCoreImage:(UIImage *)sourceImage
{
CIImage *inputImage = [CIImage imageWithCGImage:sourceImage.CGImage];
// Apply Affine-Clamp filter to stretch the image so that it does not
// look shrunken when gaussian blur is applied
CGAffineTransform transform = CGAffineTransformIdentity;
CIFilter *clampFilter = [CIFilter filterWithName:@"CIAffineClamp"];
[clampFilter setValue:inputImage forKey:@"inputImage"];
[clampFilter setValue:[NSValue valueWithBytes:&transform objCType:@encode(CGAffineTransform)] forKey:@"inputTransform"];
// Apply gaussian blur filter with radius of 30
CIFilter *gaussianBlurFilter = [CIFilter filterWithName: @"CIGaussianBlur"];
[gaussianBlurFilter setValue:clampFilter.outputImage forKey: @"inputImage"];
[gaussianBlurFilter setValue:@10 forKey:@"inputRadius"];
CIContext *context = [CIContext contextWithOptions:nil];
CGImageRef cgImage = [context createCGImage:gaussianBlurFilter.outputImage fromRect:[inputImage extent]];
// Set up output context.
UIGraphicsBeginImageContext(self.frame.size);
CGContextRef outputContext = UIGraphicsGetCurrentContext();
// Invert image coordinates
CGContextScaleCTM(outputContext, 1.0, -1.0);
CGContextTranslateCTM(outputContext, 0, -self.frame.size.height);
// Draw base image.
CGContextDrawImage(outputContext, self.frame, cgImage);
// Apply white tint
CGContextSaveGState(outputContext);
CGContextSetFillColorWithColor(outputContext, [UIColor colorWithWhite:1 alpha:0.2].CGColor);
CGContextFillRect(outputContext, self.frame);
CGContextRestoreGState(outputContext);
// Output image is ready.
UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return outputImage;
}
不幸的是,当我尝试滚动UITableView
时,这给我带来了巨大的性能问题.
Unfortunately this gives me huge performance issues when I try to scroll the UITableView
.
所以我问我如何解决?我可以使用某些库来进行像GPUImage
这样的模糊处理,我想它会更快,但是我不知道这是否可以带来很大的不同.
So I asked me how to workaround ? I could use some library to do the blurring like GPUImage
which will be faster I guess, but I don't know if this can make a big difference.
我认为UITableView
将包含大约 20-60行.
还有其他诸如缓存之类的想法吗?
Any other ideas like caching or something else ?
推荐答案
我将在设置中使用 FXBlurView 动态= NO.它具有不错的性能.
I will use FXBlurView with setting dynamic = NO. It has decent performance.
这篇关于UITableViewCell中的图像模糊时的性能问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!