4

I am trying to add a CIFilter to the backgroundFilters property of a CALayer to let this draw in a UIView. Therefore I subclassed CALayer and in the init execute the following:

CIFilter* blur = [CIFilter filterWithName:@"CIGaussianBlur"];
[blur setDefaults];
[blur setValue:@(10) forKey:@"inputRadius"];
self.backgroundFilters = @[ blur ];

Then I created a subclass of UIView that has this custom Layer as it's layer by returning the layers class in +[UIView layerClass]

This is working, the above code is executed!

However if I place this view above a UIImageView I would expect the image to be drawn blured where I put this view. But it doesn't! The view just behaves like a regular view and takes any color / alpha value I set to it's backgroundColor property, but I just see the underlaying UIImageView as it is, without a blur!

Michael Ochs
  • 2,846
  • 3
  • 27
  • 33
  • Did you find how to do it? – Lucas Jul 25 '13 at 18:38
  • I wrote this: http://ios-coding.com/bcblurview-drawing-glass-like-views/ (note that the bluring is for still images and you may be able to significantly speed this up by using the Accelerate framework) – Michael Ochs Jul 26 '13 at 07:09

2 Answers2

9

The reason for CALayer's backgroundFilters doing nothing on UIView is that unfortunately the backgroundFilters property is not supported on layers in iOS. It's inside inside the documentation, at the very end of the description:

Special Considerations

This property is not supported on layers in iOS.

I got bitten by this myself trying to insert a UIView that blurs the contents behind it. To blur a view on iOS, some more work seems to be required (copying the UIViews content inside an image, then blurring and showing the image).

Tammo Freese
  • 10,514
  • 2
  • 35
  • 44
  • Yes, I found that by myself after a while. Sorry I didn't update this post. I wrote a blog post about the issue and also a small open source component: http://ios-coding.com/bcblurview-drawing-glass-like-views/ – Michael Ochs Jun 06 '13 at 15:58
  • 3
    And yet at the top of the very same document it states in the Availability section: iOS 2.0+ – Dave Aug 27 '20 at 06:28
-2

I think that is the expected behaviour.

From the docs:

Background filters affect the content behind the layer that shows through into the layer itself. Typically this content belongs to the superlayer that acts as the parent of the layer.

My interpretation is that these filters only affect content in the view that they are contained in.

sosborn
  • 14,676
  • 2
  • 42
  • 46
  • But the layer's superlayer is the layer of the views superview? Actually this is exactly the reason why I thought this would work? – Michael Ochs Apr 27 '13 at 21:58
  • The layer only belongs to one view. That view is what would get filtered. – sosborn Apr 28 '13 at 00:02
  • But the `CALayer` doesn't know anything about the `UIView` that created it. All it knows is, that there is a `superlayer` it is attached to. The layer shouldn't care if this `superlayer` was created by another `UIView` instance!? – Michael Ochs Apr 28 '13 at 15:30
  • `However if I place this view above a UIImageView` Correct me if I am wrong, but you have your UIView subclass with the filters. This works. Now you put your UIView over a UIImageView and you expect the UIImageView to be blurred, right? That won't work because your filters are contained in your UIView. It knows nothing of the UIImageView and will not affect it. – sosborn Apr 28 '13 at 17:25