Frosted Glass (iOS 7 Blur) Effect

前端 未结 4 786
Happy的楠姐
Happy的楠姐 2020-12-07 09:51

I am trying to apply a frosted glass effect in a UIImageView.

I tried to implement what I\'ve found in this question, but the result wasn\'t accepta

4条回答
  •  臣服心动
    2020-12-07 10:22

    Solution for iOS 7 and 8, without using CoreImage or CoreGraphics at all:

    - (void)addBlurToView:(UIView *)view {
        UIView *blurView = nil; 
    
        if([UIBlurEffect class]) { // iOS 8
            UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight];
            blurView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
            blurView.frame = view.frame;
    
        } else { // workaround for iOS 7
            blurView = [[UIToolbar alloc] initWithFrame:view.bounds];
        }
    
        [blurView setTranslatesAutoresizingMaskIntoConstraints:NO];
    
        [view addSubview:blurView];
        [view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[blurView]|" options:0 metrics:0 views:NSDictionaryOfVariableBindings(blurView)]];
        [view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[blurView]|" options:0 metrics:0 views:NSDictionaryOfVariableBindings(blurView)]];
    }
    

    (assuming you're not targeting versions older than iOS 7; if you are, you'll have to test for the iOS version in the else block)

    This solution applies to any view, not only images.

提交回复
热议问题