我需要用纯色覆盖一个小的 png。 我使用的代码是:
- (UIImage *)overlayWithColor:(UIColor *)overlayColor {
UIGraphicsBeginImageContext(self.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[overlayColor setFill];
CGContextTranslateCTM(context, 0, self.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextSetBlendMode(context, kCGBlendModeNormal);
CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);
CGContextDrawImage(context, rect, self.CGImage);
CGContextClipToMask(context, rect, self.CGImage);
CGContextAddRect(context, rect);
CGContextDrawPath(context,kCGPathFill);
UIImage *coloredImg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return coloredImg;
}
但结果不是很平滑...这里是原始图像(黑色)和叠加图像(白色)
最佳答案
我的做法如下(假设您是 iOS7-only): 反转原始图像:在 Photoshop 或类似工具中对其进行编辑,使黑线透明(alpha),而空白则不透明(它是什么颜色都无所谓,只要它不透明即可)。 然后在代码中:
imageView.image = [imageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
[imageView.image setTintColor:[UIColor orangeColor]];
其中 imageView 是包含您的图像的 View 。
关于ios - UIImage 覆盖颜色 : bad result,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21723496/