I have an image in my iPad app and I basically want to place a color filter on top of it. For this I have a colored UIView that is masked to a certain shape that I put over the image. Since adjusting the alpha isn't giving me the desired effect I'd like to use blend modes instead.
As far as I know you can only use blend modes on images and not plain views. The color of the view is dynamic so I can't just use a picture instead.
I also tried rasterizing the view to an image, but that got all pixely and odd or something, but maybe I did something wrong.
So, the basic question is: Is it possible to apply blend modes to views? Or should I take a completely different approach to reach the same goal?
Take a look at the docs for CALayer's compositingFilter: https://developer.apple.com/documentation/quartzcore/calayer/1410748-compositingfilter
On your color overlay view you can set
view.layer.compositingFilter
to a CICategoryCompositeOperation to achieve blending with the content behind it. Here is some sample playground code with a multiply blend mode applied to test it out (replace theUIImage(named: "")
with your own image for testing)iOS 13, Swift 5
The key is setting the
compositingFilter
property on the appropriate CALayer (the one on top - it blends with whatever is behind it). Color blending works between UIViews or CALayers. You can find blending modes here CICategoryCompositeOperation. To use a filter, just drop theCI
& lowercase the first letter of the name (e.g.CIDivideBlendMode
becomesdivideBlendMode
). Here is some playground code to illustrate:The result looks like this:
I confirmed that the same code works in iOS. Here's the view controller code:
And here's the result in the simulator: