12

I have a UIImageView and want it to be displayed with a specific blend mode. I know the iPhone has different blend modes, and it's probably possible to do it with a lot of CG code… but maybe there's a nice way with the CALayer of UIImageView?

Andriy
  • 2,767
  • 2
  • 21
  • 29
dontWatchMyProfile
  • 45,440
  • 50
  • 177
  • 260

2 Answers2

7

Set the compositingFilter of a view's layer to a supported blend mode string. From the docs, a layer's compositingFilter is

A CoreImage filter used to composite the layer and the content behind it.

To obtain a list of Core Image filters, print out the filter names defined by a kCICategoryCompositeOperation

[CIFilter filterNamesInCategory:kCICategoryCompositeOperation]

or directly as

[CIFilter filterNamesInCategory:@"CICategoryCompositeOperation"]

The array will include Core Image filters in the form

{
   CIColorBlendMode,
   CIColorBurnBlendMode,
   CIColorDodgeBlendMode,
   CIMultiplyBlendMode,
   ...
}

To use the CIMultiplyBlendMode, set "multiplyBlendMode" as the compositingFilter on the layer

self.layer.compositingFilter = @"multiplyBlendMode";
bdev
  • 2,060
  • 5
  • 24
  • 32
  • 3
    Complete list of filters is [here](https://developer.apple.com/library/content/documentation/GraphicsImaging/Reference/CoreImageFilterReference/index.html#//apple_ref/doc/uid/TP30000136-SW71). – bfx Feb 19 '18 at 13:27
  • 6
    Since the question is about iOS, beware, though, that Apple states in the documentation for `compositingFilter`: "This property is not supported on layers in iOS." – bfx Feb 19 '18 at 13:33
  • 4
    Strange that the documentation says that it's not supported on iOS, but it does appear to work as shown by this sample project: https://github.com/arthurschiller/CompositingFilters – Luke Rogers May 21 '18 at 09:54
  • This definitely works. I was able to recreate this from the github project listed above. I think the key factor that make this work is the hierarchy of the views: the subview is the one that needs the `compositingFilter`, not the parent view. – Merricat Sep 23 '20 at 19:28
  • Did you set the imageView layer? – João Nunes Aug 31 '23 at 13:44
1

see here: composite colors: CALayer and blend mode on iPhone

Community
  • 1
  • 1
humasect
  • 552
  • 5
  • 9