Skip to content

Instantly share code, notes, and snippets.

@vaghul
Created September 21, 2016 12:17

Revisions

  1. vaghul created this gist Sep 21, 2016.
    59 changes: 59 additions & 0 deletions blur view
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,59 @@

    up vote
    109
    down vote
    accepted
    With the release of iOS 8.0, there is no need for getting an image and blurring it anymore. As Andrew Plummer pointed out, you can use UIVisualEffectView with UIBlurEffect.

    UIViewController * contributeViewController = [[UIViewController alloc] init];
    UIBlurEffect * blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    UIVisualEffectView *beView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
    beView.frame = self.view.bounds;

    contributeViewController.view.frame = self.view.bounds;
    contributeViewController.view.backgroundColor = [UIColor clearColor];
    [contributeViewController.view insertSubview:beView atIndex:0];
    contributeViewController.modalPresentationStyle = UIModalPresentationOverCurrentContext;

    [self presentViewController:contributeViewController animated:YES completion:nil];
    Solution that works before iOS 8

    I would like to extend on rckoenes' answer:

    As emphasised, you can create this effect by:

    Convert the underlying UIView to an UIImage
    Blur the UIImage
    Set the UIImage as background of your view.
    enter image description here

    Sounds like a lot of work, but is actually done pretty straight-forward:

    1. Create a category of UIView and add the following method:

    -(UIImage *)convertViewToImage
    {
    UIGraphicsBeginImageContext(self.bounds.size);
    [self drawViewHierarchyInRect:self.bounds afterScreenUpdates:YES];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
    }
    2. Make an image of the current view and blur it by using Apple's Image Effect category (download)

    UIImage* imageOfUnderlyingView = [self.view convertViewToImage];
    imageOfUnderlyingView = [imageOfUnderlyingView applyBlurWithRadius:20
    tintColor:[UIColor colorWithWhite:1.0 alpha:0.2]
    saturationDeltaFactor:1.3
    maskImage:nil];
    3. Set it as background of your overlay.

    -(void)viewDidLoad
    {
    self.view.backgroundColor = [UIColor clearColor];
    UIImageView* backView = [[UIImageView alloc] initWithFrame:self.view.frame];
    backView.image = imageOfUnderlyingView;
    backView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.6];
    [self.view addSubview:backView];
    }