我正在尝试在运行时更改图像的颜色。我在 SO 上看到了几个答案,但它们都改变了背景颜色而不是前景,这就是我想要做的。我的代码基于另一个SO 线程。
这是我的代码:
@implementation UIImage (Coloring)
-(UIImage*) imageWithColorOverlay:(UIColor*)color
{
//create context
UIGraphicsBeginImageContextWithOptions(self.size, NO, self.scale);
CGContextRef context = UIGraphicsGetCurrentContext();
// drawingcode
//bg
CGRect rect = CGRectMake(0.0, 0.0, self.size.width, self.size.height);
[self drawInRect:rect];
//fg
CGContextSetBlendMode(context, kCGBlendModeMultiply);
CGContextSetFillColorWithColor(context, color.CGColor);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//end
return image;
}
@end
这是迄今为止的结果:
从左到右:
- 没有混合,只是正常的资产。灰色背景来自
UIView
imageviews的后面,图像的背景是透明的。 - 乘以
kCGBlendModeMultiply
- 颜色燃烧与
kCGBlendModeColorBurn
有没有CGBlendMode
可以实现替换前景色的方法?另外,是否可以同时替换前景色(白色)和阴影(黑色)?