Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overlay UIImageViews with Multiply blend mode?

I have 2 UIImageViews that are shown on top of each other. One of them can be dragged around using a Gesture Recognizer.

Is there a way that the ImageViews can be rendered using a blend mode like Multiply? Such that when they move on top of each, they get rendered with that blend mode?

like image 463
Gregor Hochmuth Avatar asked Jan 30 '13 17:01

Gregor Hochmuth


People also ask

How do you Multiply blend mode?

The Multiply mode simply multiplies each component in the two layers. The Color Burn mode divides the inverted bottom layer by the top layer, and then inverts the result. This darkens the top layer increasing the contrast to reflect the color of the bottom layer. The darker the bottom layer, the more its color is used.

What is Overlay blending mode?

Overlay is another of Photoshop's most Commonly Used Blending Modes. It uses Screen at half strength on colors brighter than 50% gray. And the Multiply at half strength on colors darker than 50% gray. 50% gray itself becomes transparent.

What does Multiply blending do?

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 Blend mode in javafx?

A blending mode defines the manner in which the inputs of a Blend effect are composited together or how a Node is blended into the background of a scene.


1 Answers

You have to override the drawRect: function on the parent view, in order to achieve something like this:

   - (void)drawRect:(CGRect)rect
{
    CGContextRef ctx = UIGraphicsGetCurrentContext();

    [image1.image drawInRect:image1.frame blendMode:kCGBlendModeMultiply alpha:1];
    [image2.image drawInRect:image2.frame blendMode:kCGBlendModeMultiply alpha:1];
    [super drawRect:rect];
}

What it does is grab the current graphicsContext, and draws the two images into it, using a multiply blend mode.

To be able to see this, you'll need to set the alpha of the two images to 0, or the newly drawn content will be obscured. Since the parent view is redrawing them, you'll see the resulting multiplied versions.

Also, whenever the images' positions get updated, you'll need to call setNeedsDisplay on the parent view, to force it to call drawRect once again.

I'm certain there are probably more efficient ways to utilize Quartz 2D to achieve what you want, but this is probably the simplest.

like image 55
mjvotaw Avatar answered Nov 15 '22 15:11

mjvotaw