Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easiest/fastest way to apply a blend mode between two UIViews

I am trying to quickly invert the colors of an existing UIView (turn into a negative, as in a photographic negative). Or to take the colors and turn down the saturation. I think easier than manipulating the bitmap is to take another UIView that is all white(or shades), and to use blendmodes to achieve my goals.

I don't see a way to direct how a UIView is drawn on top of another. Even if I subclass UIView, I don't see an easy way to drawrect using a blend more. It looks like I will have to subclass UIImageView which has a drawrect:withBlendMode.

Has anyone done this in an easier way? I was hoping I could just set a blendmode property on a UIView or UIImageView and not have to subclass...

Basically, how would you quickly invert a view so that black is white and white is black?

like image 696
mahboudz Avatar asked Sep 03 '09 04:09

mahboudz


People also ask

How does Multiply blend mode work?

The Multiply mode multiplies the colors of the blending layer and the base layers, resulting in a darker color. This mode is useful for coloring shadows. The Color burn mode is named after the photography film development technique of “burning” or overexposing prints to make the colors darker.

What is mix blend mode in CSS?

The mix-blend-mode CSS property sets how an element's content should blend with the content of the element's parent and the element's background.


1 Answers

This seems to be the solution:

Crate a subclass of UIView. Then in drawRect:

    [self.image drawInRect:rect];

    // prepare the context to draw into
    CGContextRef context = UIGraphicsGetCurrentContext();

    // set the blend mode
    CGContextSetBlendMode(context, kCGBlendModeDifference);

    // now draw the appropriate color over the whole thing
    CGContextFillRect(....);

I'm off to try this out now.

like image 108
mahboudz Avatar answered Oct 20 '22 06:10

mahboudz