在 UIImageView 上使用色彩

我有自己的 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]];
}
}

我做错了什么? (图像的颜色总是保持原来的样子。)

86885 次浏览

而不是这个代码:

[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()

(无法编辑@兆龙钟贴)

在快速 3.0中,你可以做:

let image = UIImage(named: "your_image_name")!.withRenderingMode(.alwaysTemplate)


yourImageView.image = image


yourImageView.tintColor = UIColor.blue

你也可以在你的资产上设置这个。确保你的图像包含所有的白色像素 + 透明。 enter image description here

快速版本: 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]];

或者

您也可以将其设置在您的资产上。

enter image description here

更进一步。这是 UIImageView 的一个下拉子类。(原问题解答不准确)通过将类名设置为 TintedImageView 在 Interface Builder 中使用。随着色彩颜色的变化,设计器内部实时更新。

(Swift 3.1,Xcode 8.3)

import UIKit


@IBDesignable class TintedImageView: UIImageView {
override func prepareForInterfaceBuilder() {
self.configure()
}


override func awakeFromNib() {
super.awakeFromNib()


self.configure()
}


@IBInspectable override var tintColor: UIColor! {
didSet {
self.configure()
}
}


private func configure() {
self.image = self.image?.withRenderingMode(UIImageRenderingMode.alwaysTemplate)
}
}

制作图像视图

    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,或为了效率你需要渲染 ONCE (或表格视图单元格的例子)

func tint(with color: UIColor) -> UIImage {
var image = withRenderingMode(.alwaysTemplate)
UIGraphicsBeginImageContextWithOptions(size, false, scale)
color.set()


image.draw(in: CGRect(origin: .zero, size: size))
image = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return image
}

并将此 UIImage 设置为所有 UI 元素。

@ odemolliens 答案应该只是工作。

但是如果你仍然有问题,确保你正在应用于 UIImageView 的色彩不同于 Interface Builder 中定义的色彩。