我正在使用CoreGraphics(在我的drawRect方法中)尝试将混合模式应用于图像(透明png),然后调整结果的alpha值。我认为这需要分两步完成,但我可能错了。目前为止,我的代码如下(可以正常工作):
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
//SET COLOR - EDIT... added a more practical color example
CGContextSetRGBFillColor(context, 0.0, 1.0, 0.0, 1);
//flips drawing context (apparently this is necessary)
CGContextTranslateCTM(context, 0.0, self.bounds.size.height);
CGContextScaleCTM(context, 1.0, -1.0);//flip context
//DRAW PIN IMAGE
UIImage *pin = [UIImage imageNamed:@"pin"];
CGRect pinrect = CGRectMake(12, 17, 25, 25);
CGContextDrawImage(context, pinrect, pin.CGImage);//draws image in context
//Apply blend mode
CGContextSetBlendMode(context, kCGBlendModeColor);
CGContextClipToMask(context, pinrect, pin.CGImage); // restricts drawing to within alpha channel
//fills context with mask, applying blend mode
CGContextFillRect(context, pinrect);
CGContextRestoreGState(context);
// -- Do something here to make result 50% transparent ?? --
我假设我需要将所有这些绘制到某种单独的上下文中,调用
CGContextSetAlpha(...)
,然后重新将其绘制回原始上下文,但我不确定如何操作。在最终的CGContextFillRect之前设置alpha只会改变混合模式应用的数量,而不是整个图像的alpha。
编辑:已发布屏幕截图
提前致谢。