gpt4 book ai didi

objective-c - GPUImage 动画高斯模糊滤镜

转载 作者:行者123 更新时间:2023-12-02 07:34:00 25 4
gpt4 key购买 nike

我正在使用 GPUImage 高斯模糊滤镜来模糊静止图像。我想将模糊大小与 UI 元素联系起来,当用户更改该元素时,我会模糊图片。我现在的做法是在发生重大(> 0.25)变化时更改 blurSize,重新应用滤镜并将新图像动画化到 imageView 中。

有没有更有效的方法让我这样做?

在 iPhone 5 上,虽然性能并不滞后,但也不是非常流畅(但这可能是因为这种操作太昂贵而无法非常流畅)。

最佳答案

有趣的问题。你最好的选择是在你的范围内以几个模糊比例预先计算和渲染 CGImages,然后使用两个堆叠的 UIImageViews,其中一个在顶部显示模糊副本但部分透明。例如,如果您在半径 1、2、3、…、10 处渲染模糊,并且您想要显示 3.7,则应在底部显示图像 3,在顶部显示图像 4,不透明度为 70%。

// Created in answer to Nikhil Varma's question at http://stackoverflow.com/questions/18804668/gpuimage-animated-gaussian-blur-filter
#import "GPUImage.h"
#define BLUR_STEP 2.
@interface AHViewController : UIViewController

@property (weak, nonatomic) IBOutlet UIImageView *imageView2;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (nonatomic) CGFloat blurRadius;
@property (nonatomic, strong) UIImage *image;
@property (nonatomic, strong) GPUImageGaussianBlurFilter *blurFilter;
@property (nonatomic, strong) NSCache *blurredImageCache;
@property (nonatomic, strong) dispatch_queue_t blurQueue;
@property (nonatomic, strong) NSMutableSet *blurAmountsBeingRendered;
@end

@implementation AHViewController

-(void)viewDidLoad {
[super viewDidLoad];
self.image = [UIImage imageNamed:@"ph.jpg"];
self.blurFilter = [GPUImageGaussianBlurFilter new];
self.blurredImageCache = [NSCache new];
self.blurQueue = dispatch_queue_create("Image Blur Queue", DISPATCH_QUEUE_SERIAL);
self.blurAmountsBeingRendered = [NSMutableSet set];
self.blurRadius = 1.0;
}
- (IBAction)sliderDidMove:(UISlider *)sender {
self.blurRadius = 10 * sender.value;
}
-(void)setBlurRadius:(CGFloat)blurRadius {
_blurRadius = blurRadius;
CGFloat smaller = self.blurRadius - fmodf(self.blurRadius, BLUR_STEP);
[self asyncGenerateImageWithBlurAmount:smaller];
CGFloat larger = smaller + BLUR_STEP;
[self asyncGenerateImageWithBlurAmount:larger];

}
-(UIImage *)cachedImageWithBlurAmount:(CGFloat)blur {
return [self.blurredImageCache objectForKey:@(blur)];
}
-(void)asyncGenerateImageWithBlurAmount:(CGFloat)blur {
// This image is already available.
if([self.blurredImageCache objectForKey:@(blur)]) {
[self imageIsAvailableWithBlur:blur];
return;
}
// There's already a render going on for this. Just return.
if([self.blurAmountsBeingRendered containsObject:@(blur)])
return;

// Start a render
[self.blurAmountsBeingRendered addObject:@(blur)];
dispatch_async(self.blurQueue, ^{
self.blurFilter.blurSize = blur;
UIImage *result = [self.blurFilter imageByFilteringImage:self.image];
[self.blurredImageCache setObject:result forKey:@(blur)];
[self.blurAmountsBeingRendered removeObject:@(blur)];
dispatch_async(dispatch_get_main_queue(), ^{
[self imageIsAvailableWithBlur:blur];
});
});
}

-(void)imageIsAvailableWithBlur:(CGFloat)blurAmount {
CGFloat smaller = self.blurRadius - fmodf(self.blurRadius, BLUR_STEP);
CGFloat larger = smaller + BLUR_STEP;

UIImage *sharperImage = [self cachedImageWithBlurAmount:smaller];
UIImage *blurrier = [self cachedImageWithBlurAmount:larger];
if(sharperImage && blurrier) {
if(![self.imageView.image isEqual:sharperImage])
self.imageView.image = sharperImage;
if(![self.imageView2.image isEqual:blurrier]) {
self.imageView2.image = blurrier;
}
self.imageView2.alpha = (self.blurRadius - smaller) / BLUR_STEP;
}
}


@end

关于objective-c - GPUImage 动画高斯模糊滤镜,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18804668/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com