3

我想将 r、g、b 和两个 CGImage 的值相乘,以使用单个图像作为蒙版(在其 alpha 通道中)和色调(在 rgb 通道中)。我首先简单地尝试了:

CGContextDrawImage(context, CGRectMake(0, 0, _w, _h), handle());
CGContextSetBlendMode(context, kCGBlendModeMultiply);
CGContextDrawImage(context, CGRectMake(0, 0, other->width(), other->height()), over);

但是,这只会着色,不会遮盖。因此,我将遮罩图像中的 alpha 提取到 CGImage 遮罩灰度图像中(使用 CGDataProvider)并将其用作上下文中的遮罩:

// "mask" is "other"'s alpha channel as an inverted grayscale image
CGContextSaveGState(context);
CGContextClipToMask(context, CGRectMake(0, 0, other->width(), other->height()), mask);
CGImageRelease(mask);
CGContextDrawImage(context, CGRectMake(0, 0, _w, _h), handle());
CGContextRestoreGState(context); // Don't apply the clip mask to 'other', since it already has it
CGContextSetBlendMode(context, kCGBlendModeMultiply);
CGContextDrawImage(context, CGRectMake(0, 0, other->width(), other->height()), other->handle());

但是,它仍然看起来不正确。图像变;相乘的图像是否应该总是至少更暗?图像预览在:

http://dl.dropbox.com/u/6775/multiply/index.html

提前感谢您的启发:)

4

1 回答 1

2

第一件事是您不需要将 Alpha 通道提取到单独的通道来进行遮罩。你可以用kCGBlendModeDestinationIn. 对于倒置蒙版,同样的事情,但使用kCGBlendModeDestinationOut.

因此,解决方案是使用 Multiply 绘制第二张图像,然后使用 Destination In 或 Out 再次绘制它。

于 2010-09-16T18:16:17.237 回答