代码之家  ›  专栏  ›  技术社区  ›  nevyn

将RGBA与两个cgimageref相乘

  •  3
  • nevyn  · 技术社区  · 14 年前

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

    但是,这只会着色,而不会遮罩。因此,我将遮罩图像中的alpha提取为CGImage mask灰度图像(使用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

    提前感谢您的启示:)

    1 回复  |  直到 14 年前
        1
  •  2
  •   Peter Hosey    14 年前

    首先,不需要将alpha通道提取到单独的通道来进行掩蔽。你可以用它来画 kCGBlendModeDestinationIn . 对于一个倒置的面具,同样的事情,但是 kCGBlendModeDestinationOut .

    所以,解决方法是用乘法绘制第二幅图像,然后用目标输入或输出再次绘制。