Ios 在UIImageView上使用着色颜色
我有自己的子类Ios 在UIImageView上使用着色颜色,ios,xcode,uiimageview,uibutton,tintcolor,Ios,Xcode,Uiimageview,Uibutton,Tintcolor,我有自己的子类UIButton。我在上面添加了UIImageView,并添加了一个图像。我想在图像上涂上一层浅色,但不起作用 到目前为止,我已经: - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { self.backgroundColor = [UIColor clearColor]; self.clipsToBounds = Y
UIButton
。我在上面添加了UIImageView
,并添加了一个图像。我想在图像上涂上一层浅色,但不起作用
到目前为止,我已经:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor clearColor];
self.clipsToBounds = YES;
self.circleView = [[UIView alloc]init];
self.circleView.backgroundColor = [UIColor whiteColor];
self.circleView.layer.borderColor = [[Color getGraySeparatorColor]CGColor];
self.circleView.layer.borderWidth = 1;
self.circleView.userInteractionEnabled = NO;
self.circleView.translatesAutoresizingMaskIntoConstraints = NO;
[self addSubview:self.circleView];
self.iconView = [[UIImageView alloc]init];
[self.iconView setContentMode:UIViewContentModeScaleAspectFit];
UIImage * image = [UIImage imageNamed:@"more"];
[image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
self.iconView.image = image;
self.iconView.translatesAutoresizingMaskIntoConstraints = NO;
[self.circleView addSubview:self.iconView];
...
关于选择:
- (void) setSelected:(BOOL)selected
{
if (selected) {
[self.iconView setTintColor:[UIColor redColor]];
[self.circleView setTintColor:[UIColor redColor]];
}
else{
[self.iconView setTintColor:[UIColor blueColor]];
[self.circleView setTintColor:[UIColor blueColor]];
}
}
我做错了什么?(图像的颜色始终保持原来的颜色。)而不是此代码:
[image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
你应该:
image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
在Swift 4.1中使用此选项
image = UIImage(named: "name")!.withRenderingMode(.alwaysTemplate)
在swift 2.0+中,您可以执行以下操作:
let image = UIImage(named: "your_image_name")!.imageWithRenderingMode(.AlwaysTemplate)
yourImageView.image = image
yourImageView.tintColor = UIColor.blueColor()
let image = UIImage(named: "your_image_name")!.withRenderingMode(.alwaysTemplate)
yourImageView.image = image
yourImageView.tintColor = UIColor.blue
(无法编辑@Zhaolong Zhong post)
在swift3.0中,您可以执行以下操作:
let image = UIImage(named: "your_image_name")!.imageWithRenderingMode(.AlwaysTemplate)
yourImageView.image = image
yourImageView.tintColor = UIColor.blueColor()
let image = UIImage(named: "your_image_name")!.withRenderingMode(.alwaysTemplate)
yourImageView.image = image
yourImageView.tintColor = UIColor.blue
你也可以在你的资产上设置它。确保您的图像包含所有白色像素+透明。
Swift版本:5.2
let tintableImage = UIImage(named: "myImage")?.withRenderingMode(.alwaysTemplate)
imageView.image = tintableImage
imageView.tintColor = UIColor.red
目标C
self.imgView.image = [self.imgView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
[self.imgView setTintColor:[UIColor darkGrayColor]];
或
您也可以在资产上设置此选项。
更进一步。这是UIImageView的一个子类。(不是原始问题的精确解决方案。)通过将类名设置为TintedImageView在Interface Builder中使用。随着色调颜色的更改,在设计器中实时更新 (Swift 3.1,Xcode 8.3) 创建imageView
let imageView = UIImageView(frame: frame!)
imageView.contentMode = .ScaleAspectFit
imageView.tintColor = tintColor
制作图像
let mainBundle = NSBundle.mainBundle()
var image = UIImage(named: filename!, inBundle: mainBundle, compatibleWithTraitCollection: nil)
image = image?.imageWithRenderingMode(.AlwaysTemplate)
把它们连在一起
imageView?.image = image
展示它
view.addSubview(imageView)
所说的都是对的。如果您不能/不想应用于每个UiImageView,或者为了提高效率,您需要渲染一次(或者以TableView的单元格为例),我的贡献
并将此UIImage设置为所有UI元素。@Odemollien的答案应该可以正常工作
但是,如果仍然存在问题,请确保应用于UIImageView的着色颜色与Interface Builder中定义的颜色不同 在创建iconView时,您是否能够
setTintColor
?您的意思是在self.iconView=[UIImageView alloc]。。。?是的,我可以,但它不起作用。那么使用CGContext。也许你可以在这里找到答案是的,我看到了这篇文章,但我真的不明白为什么我的代码不起作用。使用淡色是更干净的方法。这不是一个愚蠢的错误,而是一个非常重要的细节。谢谢你发布这个。你帮我节省了很多时间。(更正的打字错误)您可以在资源中选择图像,并在右面板中选择“默认渲染模式”!但是如何恢复到原始的未着色图像?如果要获得未着色图像,可以:将着色图像存储在不同的变量中:UIImage image2=[image imageWithR….],或者可以再次从文件加载图像:image=[UIImage imagename:@“more”];我发现UIImageRenderingModeAlwaysTemplate存在奇怪的问题。如果在iOS设置>显示和亮度中启用“粗体文本”,则imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate会导致内存泄漏(图像数据永远不会释放)。这可能是系统错误,在其他一些iOS设置中也可能发生。我建议使用iOS 13中添加的“func with tintColor(color:UIColor)->UIImage”而不是.tintColor。我做了所有这些,但出于某种原因,tintColor对我不起作用。你知道我还能尝试什么吗?你用的是什么样的图像格式?图像全是白色+阿尔法吗?奇怪的是,在我的前几次跑步中,只有2/3的图像选择了淡色。在清理和构建所有图像后,所有图像都选择了色调。也许你可以向Apple?香蕉报告,这是一个已知的图像不使用色调的问题。不久前,我曾向苹果公司报告过,他们将其标记为复制品,所以他们肯定知道。解决方法是不要将UIImageView设置为故事板上的图像。因此,只需拥有一个空白的UIImageView,然后在viewDidLoad(或其他地方)中设置它,然后您将在图像上看到tintColor。不工作,但它将func配置修改为:self.image=self.image?。withRenderingMode(UIImageRenderingMode.alwaysTemplate)设color=super.tintColor super.tintColor=UIColor.clear super.tintColor=color