作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一组黑白图像,如下所示。如果我没记错的话,有一些方法可以设置 UIImage 的混合或 mask 属性以将其与背景 UIView 混合。
在这种情况下,我想将此图像的颜色更改为红色以表示生命值。 如何以编程方式更改此 UIImage 的颜色?
我知道图像过滤器的使用,特别是色调偏移,但它们确实是资源密集型的,而且它们不适用于白色图像,所以我正在寻找另一种解决方案。
我尝试用 alpha 0.4 覆盖 UIView 并将其背景颜色设置为红色,结果如下:
这在某些情况下可能有效,但在我的情况下,我想更好地控制我得到的颜色阴影。
最佳答案
您可以在此图像的基础上绘制新图像。下面的代码会将图像中的白色与您提供的背景颜色混合。百分比是一个 0-1.0 float ,用于控制从底部开始的图像中有多少与您选择的颜色混合,因此可以获得像心脏充满红色以表示生命值的结果。
-(UIImage*)blendImage:(UIImage*)image withColor:(UIColor*)color fillUpTo:(float)percent
{
//math got me
float realPercent = 1.0-percent;
CGSize size = image.size;
UIGraphicsBeginImageContext(size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, color.CGColor);
CGContextFillRect(context, CGRectMake(0, size.height*realPercent, size.width, size.height*(1-realPercent)));
//make sure the image is not upside down
CGContextTranslateCTM(context, 0, image.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
//draw image by blending colors
CGContextSetBlendMode(context, kCGBlendModeMultiply);
CGContextDrawImage(context, CGRectMake(0, 0, size.width, size.height), image.CGImage);
UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return resultImage;
}
0.3% 的结果:
关于iphone - 如何以编程方式为黑/白 UIImageView 着色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20673209/
在 Rails 中,您可以使用嵌套路由为 has_one 和 has_many 关系创建 RESTful 路由。可以在 Rails Guides 上找到示例 请问有没有什么好的方法可以为habtm关系
我是一名优秀的程序员,十分优秀!