iOS开发记录iOS学习开发iOS各问题归类

利用UIToolBar做高斯模糊背景

2019-03-05  本文已影响28人  iOS猿_员

效果图:

- (UIView *)containerBackgroundView {
    if (!_containerBackgroundView) {
        UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectZero];
        toolBar.barStyle = UIBarStyleBlack;
        toolBar.clipsToBounds = YES;
        _containerBackgroundView = toolBar;
    }
    return _containerBackgroundView;
}

也可以使用UIBlurEffect

UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
UIVisualEffectView *blurView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
blurView.frame = myView.bounds;
[myView addSubview:blurView];

UIBlurEffectStyle

UIBlurEffect 不能调节模糊半径

如果要调整模糊半径

可以对图片进行高斯模糊

-(UIImage *)convertToBlurImage:(UIImage *)image{
    CIFilter *gaussianBlurFilter = [CIFilter filterWithName:@"CIGaussianBlur"];
    [gaussianBlurFilter setDefaults];
    CIImage *inputImage = [CIImage imageWithCGImage:[image CGImage]];
    [gaussianBlurFilter setValue:inputImage forKey:kCIInputImageKey];
    [gaussianBlurFilter setValue:@5 forKey:kCIInputRadiusKey];
    CIImage *outputImage = [gaussianBlurFilter outputImage];
    CIContext *context   = [CIContext contextWithOptions:nil];
    CGImageRef cgimg     = [context createCGImage:outputImage fromRect:[inputImage extent]];  // note, use input image extent if you want it the same size, the output image extent is larger
    UIImage *convertedImage = [UIImage imageWithCGImage:cgimg];
    return convertedImage;
}

核心代码是[gaussianBlurFilter setValue:@5 forKey:kCIInputRadiusKey];

但是我测试100也没啥问题 没有测试出最大值

以上是几种高斯模糊的相关代码


文章推荐

iOS面试题大全(附答案)

文章来源于网络,如有侵权请联系小编删除

上一篇 下一篇

猜你喜欢

热点阅读