ios 如何更改图像 tintColor

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28427935/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 04:46:45  来源:igfitidea点击:

How can I change image tintColor

iosobjective-cuiimageuicolortintcolor

提问by user2168496

I'm receiving image from a server, then based on a color chosen by the user, the image color will be changed.

我从服务器接收图像,然后根据用户选择的颜色,图像颜色将发生变化。

I tried the following :

我尝试了以下方法:

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

i got the opposite of my goal (the white color outside UIImage is colored with the chosen color).

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

what is going wrong?

出了什么问题?

i need to do the same in this question,the provided solution doesn't solve my case. How can I change image tintColor in iOS and WatchKit

我需要在这个问题中做同样的事情,提供的解决方案不能解决我的情况。 如何在 iOS 和 WatchKit 中更改图像 tintColor

回答by Tony

Try to generate new image for yourself

尝试为自己生成新图像

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;

And use it.

并使用它。

Good luck

祝你好运

======= UPDATE =======

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

This solution will onlychange color of all pixel's image.

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

Example: we have a book image: http://pngimg.com/upload/book_PNG2113.png

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

enter image description here

在此处输入图片说明

And after running above code (exp: TintColoris RED). We have:

在运行上面的代码之后(exp:TintColor是红色)。我们有:

enter image description here

在此处输入图片说明

SO: how your image is depends on how you designed it

所以:你的形象如何取决于你如何设计它

回答by Mejdi Lassidi

In Swift you can use this extension: [Based on @VietHung's objective-c solution]

在 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
    }
}

回答by Ruchin Somal

In swift 2.0 you can use this

在 swift 2.0 中你可以使用这个

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

In swift 3.0 you can use this

在 swift 3.0 中你可以使用这个

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

回答by Gismay

Try something like this

尝试这样的事情

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;

Hope this helps.

希望这可以帮助。

回答by odemolliens

In Swift 3.0you can use this extension: [Based on @VietHung's objective-c solution]

在 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
    }
}

回答by tylerwalker

For Swift 3.0, I made a custom subclass of UIImageView called TintedUIImageView. Now the image uses whatever tint color is set in interface builder or code

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

class TintedUIImageView: UIImageView {

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

回答by gabriel

You can try:

你可以试试:

 _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;
}

回答by Micky

Try setting the tint color on the superview of the image view. E.g. [self.view setTintColor:color];

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

回答by Mojtaba Hosseini

- SWIFT 4

- 快速 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
    }
}

回答by Hemang

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