Search code examples
ioscore-animationquartz-core

How to blur image using CALayer


The following method attempts to apply gausian blur to an image. However it isn't doing anything. Can you please tell me what is wrong, and if you also know the reason why it's wrong, that would also help. I am trying to learn about CALayers and quartzcore.

Thanks

-(void)updateFavoriteRecipeImage{

    [self.favoriteRecipeImage setImageWithURL:[NSURL URLWithString:self.profileVCModel.favoriteRecipeImageUrl] placeholderImage:[UIImage imageNamed:@"miNoImage"]];

    //Set content mode
    [self.favoriteRecipeImage setContentMode:UIViewContentModeScaleAspectFill];
    self.favoriteRecipeImage.layer.masksToBounds = YES;

    //Blur the image
    CALayer *blurLayer = [CALayer layer];
    CIFilter *blur = [CIFilter filterWithName:@"CIGaussianBlur"];
    [blur setDefaults];
    blurLayer.backgroundFilters = [NSArray arrayWithObject:blur];
    [self.favoriteRecipeImage.layer addSublayer:blurLayer];


    [self.favoriteRecipeImage setAlpha:0];

    //Show image using fade
    [UIView animateWithDuration:.3 animations:^{

        //Load alpha
        [self.favoriteRecipeImage setAlpha:1];
        [self.favoriteRecipeImageMask setFrame:self.favoriteRecipeImage.frame];
    }];
}

Solution

  • The documentation of the backgroundFilters property says this:

    Special Considerations

    This property is not supported on layers in iOS.

    As of iOS 6.1, there is no public API for applying live filters to layers on iOS. You can write code to draw the underlying layers to a CGImage and then apply filters to that image and set it as your layer's background, but doing so is somewhat complex and isn't “live” (it doesn't update automatically if the underlying layers change).