【发布时间】:2010-09-16 09:38:34
【问题描述】:
我想将两个 CGImage 的 r、g、b 和 a 值相乘,以使用单个图像作为遮罩(在其 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
提前感谢您的启发:)
【问题讨论】:
标签: core-graphics cgcontext cgimage masking