在UITableViewCell中模糊图像时的性能问题

时间:2015-04-05 21:30:25

标签: ios objective-c iphone uitableview image-processing

我的自定义UIImageView中有UITableViewCell。包含的图像应该模糊。我知道UIVisualEffectsView,但首先在iOS8之前无法使用此功能,其次,对于我的用例,模糊有点重。

这就是我提出这个解决方案的原因:

示例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时,这给我带来了巨大的性能问题。

所以我问我如何解决?我可以使用一些库来模糊GPUImage,我想这会更快,但我不知道这是否会产生很大的影响。

我认为UITableView将包含 20-60行

任何其他想法,如缓存或其他什么?

4 个答案:

答案 0 :(得分:1)

没有理由继续计算模糊的背景图像。由于一切都使用相同的,只需将其模糊一次,并在配置单元格时分配它:

if (cell == nil)
{
    [tableView registerNib:[UINib nibWithNibName:@"DEShowCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:cellIdentifier];
    cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    // Inside the initial configuration; not outside.
    [cell.backgroundImageView setImage:self.blurredBackgroundImage];
}

一次设置self.blurredBackgroundImage

- (void)viewDidLoad {
    self.blurredBackgroundImage = [self blurWithCoreImage:[UIImage imageNamed:...]];
}

当然,鉴于此代码,我可能只是在PhotoShop中模糊图像并将其保存为资源。没有理由以编程方式模糊静态资源(除非您在其他地方使用相同的图像,并且它足够大以至于磁盘空间很重要)。

答案 1 :(得分:1)

我将使用FXBlurView设置dynamic = NO。它有不错的表现。

答案 2 :(得分:1)

其中一个WWDC 2013 code samples,iOS_UIImageEffects,通过使用vImage高性能框架来模糊图像。当我测试它时,它比CoreImage快得多。

但是在回答你关于缓存的问题时,是的,绝对应用缓存。使用NSCache作为主缓存,将持久内存用作辅助缓存。


顺便说一下,你似乎为每个细胞模糊了相同的图像。如果是这样,您应该将其模糊一次,然后为所有单元格重复使用该模糊图像。

同样,您多次注册NIB。预先注册一次。

答案 3 :(得分:0)

如果您确实希望使用CoreImage,则不应经常创建您将要使用它的上下文。您应该创建一次上下文并重新使用它,因为不这样做会导致这种性能问题。

您可以通过使用time profiler工具分析您的代码来确定上述代码中的问题是否存在,或者是否是其他问题。要做到这一点,只需从Xcode的“产品”菜单中选择“配置文件”,当它运行时,选择“Time Profiler”。然后,您将能够看到占用时间最多的调用的堆栈跟踪。

相关问题