Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Applying CALayer compositingFilter correctly on OS X?

I have an NSView. It has a layer. Inside of the layer, there are several sublayers. One of those sublayers needs to blend with the layers beneath it. I've set the top layer's compositingFilter property, but no blending happens. :-(

Here's a visual representation:

Root Layer      <-- myView.layer
|- Bottom Layer <-- myView.layer.sublayers[0]
|- Middle Layer <-- myView.layer.sublayers[1]
|- Top Layer    <-- myView.layer.sublayers[2]

Root Layer
|- Bottom Layer
|- Middle Layer
|- Top Layer    <-- Added filter to this layer.

I set the property like so:

self.topLayer.compositingFilter = [CIFilter filterWithName:@"CIMultiplyBlendMode"];

I assumed that the input image and background image would be supplied automatically with the contents of each of the layers, but maybe I'm wrong. Am I?

TL;DR: How does a n00b composite a stack of layers together using blending modes?

P.S. I'm drawing the contents of each layer using the delegate method, -drawLayer:inContext:.

like image 211
Ben Stock Avatar asked Jan 24 '26 17:01

Ben Stock


1 Answers

There is a flag you have to enable on NSView for it use filters on the layer:

[self.view setWantsLayer:YES];
[self.view setLayerUsesCoreImageFilters:YES];

I think this is specific to OS X and NSView and isn't necessary with UIView on iOS. Everything else looks correct to me.

like image 86
lintmachine Avatar answered Jan 27 '26 11:01

lintmachine



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!