我想将 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
提前感谢您的启发:)