【发布时间】:2023-04-04 06:00:01
【问题描述】:
我的自定义 UITableViewCell 中有一个 UIImageView。包含的图像应该是模糊的。我知道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 行。
还有其他想法,比如缓存或其他什么?
【问题讨论】:
标签: ios objective-c iphone uitableview image-processing