如何更改图像tintColor

时间:2015-02-10 09:18:15

标签: ios objective-c uiimage uicolor tintcolor

我正在从服务器接收图像,然后根据用户选择的颜色,图像颜色将会改变。

我尝试了以下内容:

_sketchImageView.image = [_sketchImageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
[_sketchImageView setTintColor:color];

我与我的目标相反(UIImage外面的白色用所选颜色着色)。

出了什么问题?

我需要在这个问题上做同样的事情,提供的解决方案并不能解决我的问题。 How can I change image tintColor in iOS and WatchKit

15 个答案:

答案 0 :(得分:51)

尝试为自己生成新图像

UIImage *newImage = [_sketchImageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
UIGraphicsBeginImageContextWithOptions(image.size, NO, newImage.scale);
[yourTintColor set];
[newImage drawInRect:CGRectMake(0, 0, image.size.width, newImage.size.height)];
newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

_sketchImageView.image = newImage;

并使用它。

祝你好运

=======更新=======

此解决方案更改所有像素图像的颜色。

示例:我们有一个图书图片:http://pngimg.com/upload/book_PNG2113.png

enter image description here

运行上面的代码后(exp:TintColor为RED)。我们有:

enter image description here

SO:你的形象取决于你的设计方式

答案 1 :(得分:18)

在Swift中,您可以使用此扩展程序:[基于@ VietHung' objective-c解决方案]


extension UIImage {
    func imageWithColor(color: UIColor) -> UIImage? {
        var image = imageWithRenderingMode(.AlwaysTemplate)
        UIGraphicsBeginImageContextWithOptions(size, false, scale)
        color.set()
        image.drawInRect(CGRect(x: 0, y: 0, width: size.width, height: size.height))
        image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image
    }
}

答案 2 :(得分:17)

在swift 2.0中你可以使用这个

let image = UIImage(named:"your image name")?.imageWithRenderingMode(.AlwaysTemplate)
let yourimageView.tintColor = UIColor.redColor()
yourimageView.image = image

在swift 3.0中,你可以使用这个

let image = UIImage(named:"your image name")?.withRenderingMode(.alwaysTemplate)
let yourimageView.tintColor = UIColor.red
yourimageView.image = image

答案 3 :(得分:6)

尝试这样的事情

UIImage *originalImage = _sketchImageView.image
UIImage *newImage = [originalImage imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,50,50)]; // your image size
imageView.tintColor = [UIColor redColor];  // or whatever color that has been selected
imageView.image = newImage;
_sketchImageView.image = imageView.image;

希望这有帮助。

答案 4 :(得分:4)

在Swift 3.0 中,您可以使用此扩展程序:[基于@ VietHung的objective-c解决方案]

extension UIImage {
    func imageWithColor(_ color: UIColor) -> UIImage? {
        var image = imageWithRenderingMode(.alwaysTemplate)
        UIGraphicsBeginImageContextWithOptions(size, false, scale)
        color.set()
        image.draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height))
        image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image
    }
}

答案 5 :(得分:2)

您可以尝试:

 _sketchImageView.image = [self imageNamed:@"imageName" withColor:[UIColor blackColor]];

 - (UIImage *)imageNamed:(NSString *)name withColor:(UIColor *)color
 {
     // load the image
     //NSString *name = @"badge.png";
     UIImage *img = [UIImage imageNamed:name];

     // begin a new image context, to draw our colored image onto
     UIGraphicsBeginImageContext(img.size);

     // get a reference to that context we created
     CGContextRef context = UIGraphicsGetCurrentContext();

     // set the fill color
     [color setFill];

    // translate/flip the graphics context (for transforming from CG* coords to UI* coords
    CGContextTranslateCTM(context, 0, img.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);

    // set the blend mode to color burn, and the original image
    CGContextSetBlendMode(context, kCGBlendModeColorBurn);
    CGRect rect = CGRectMake(0, 0, img.size.width, img.size.height);
    CGContextDrawImage(context, rect, img.CGImage);

    // set a mask that matches the shape of the image, then draw (color burn) a colored rectangle
    CGContextClipToMask(context, rect, img.CGImage);
    CGContextAddRect(context, rect);
    CGContextDrawPath(context,kCGPathFill);

    // generate a new UIImage from the graphics context we drew onto
    UIImage *coloredImg = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    //return the color-burned image
    return coloredImg;
}

答案 6 :(得分:2)

对于Swift 3.0,我创建了一个名为TintedUIImageView的UIImageView的自定义子类。现在,图像使用在界面构建器或代码中设置的任何色调颜色

class TintedUIImageView: UIImageView {

    override func awakeFromNib() {
        if let image = self.image {
            self.image = image.withRenderingMode(.alwaysTemplate)
        }
    }
}

答案 7 :(得分:1)

尝试在图像视图的超级视图上设置色调颜色。例如。 [self.view setTintColor:color];

答案 8 :(得分:1)

-SWIFT 4

extension UIImage {
    func imageWithColor(_ color: UIColor) -> UIImage? {
        var image: UIImage? = withRenderingMode(.alwaysTemplate)
        UIGraphicsBeginImageContextWithOptions(size, false, scale)
        color.set()
        image?.draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height))
        image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image
    }
}

答案 9 :(得分:0)

这里是我如何使用Swift在IOS 9中应用和使用色调。

//apply a color to an image
//ref - http://stackoverflow.com/questions/28427935/how-can-i-change-image-tintcolor
//ref - https://www.captechconsulting.com/blogs/ios-7-tutorial-series-tint-color-and-easy-app-theming
func getTintedImage() -> UIImageView {

    var image     : UIImage;
    var imageView : UIImageView;

    image = UIImage(named: "someAsset")!;
    let size  : CGSize = image.size;
    let frame : CGRect = CGRectMake((UIScreen.mainScreen().bounds.width-86)/2, 600, size.width, size.height);

    let redCover : UIView = UIView(frame: frame);

    redCover.backgroundColor = UIColor.redColor();
    redCover.layer.opacity = 0.75;

    imageView       = UIImageView();
    imageView.image = image.imageWithRenderingMode(UIImageRenderingMode.Automatic);

    imageView.addSubview(redCover);

    return imageView;
}

答案 10 :(得分:0)

您可以做的一件事是,只需将您的图像添加到XCode中的Assets文件夹,然后将渲染模式更改为模板图像,因此每当您更改UIImageView的色调颜色时,它都会自动更改为图像。

检查此链接 - > https://www.google.co.in/url?sa=i&rct=j&q=&esrc=s&source=images&cd=&cad=rja&uact=8&ved=0ahUKEwiM0YXO0ejTAhUIQ48KHfGpBpgQjRwIBw&url=https%3A%2F%2Fkrakendev.io%2Fblog%2F4-xcode-asset-catalog-secrets-you-need-to-know&psig=AFQjCNGnAzVn92pCqM8612o1R0J9q1y7cw&ust=1494619445516498

答案 11 :(得分:0)

Swift 4 中,您可以简单地进行如下扩展:

import UIKit

extension UIImageView {

    func tintImageColor(color: UIColor) {
        guard let image = image else { return }
        self.image = image.withRenderingMode(UIImageRenderingMode.alwaysTemplate)
        self.tintColor = color
    }

}

答案 12 :(得分:0)

最简单的方法是使用没有目标/动作的系统类型UIButton。 作为UIButtonTypeSystem,iOS将为您进行渲染。您所要做的就是设置按钮的颜色。 无需动作的仅图像UIButton就像UIImageView一样在所有方面起作用。您可以设置约束或其他约束。

UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
[button setImage:image forState:UIControlStateNormal];
[button setImage:image forState:UIControlStateSelected];
[button setImage:image forState:UIControlStateHighlighted];
[button setTintColor:[UIColor redColor]]; // eg. set Tint color to Red

答案 13 :(得分:0)

let image = UIImage(named: "i m a g e   n a m e")?.withRenderingMode(.alwaysTemplate)
imageView.tintColor = UIColor.white // Change to require color 
imageView.image = image

答案 14 :(得分:0)

试试这个

iOS 13.4 及更高版本

UIImage *image = [UIImage imageNamed:@"placeHolderIcon"];
[image imageWithTintColor:[UIColor whiteColor] renderingMode: UIImageRenderingModeAlwaysTemplate];