【问题标题】:How can I change image tintColor如何更改图像 tintColor
【发布时间】:2024-01-14 14:17:01
【问题描述】:

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

我尝试了以下方法:

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

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

出了什么问题?

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

【问题讨论】:

  • 如果你想用一些颜色填充图像,你需要更高级的解决方案。像这样:github.com/mxcl/UIImageWithColor
  • 我觉得你需要背景色的效果,试试[_sketchImageView setBackgroundColor: color]
  • 我之前尝试过这个解决方案,它给出了相同的结果。 //UIImage *img = [UIImage resizableImageWithColor:color cornerRadius:10]; // UIImage *img = [UIImage imageWithColor:color size:CGSizeMake(20, 20)]; UIImage *img = [UIImage imageWithColor:color]; _sketchImageView.image=img;
  • @saif [_sketchImageView setBackgroundColor: [UIColor clearColor]]; _sketchImageView.image = [_sketchImageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]; [_sketchImageView setTintColor:color];给了我同样的结果

标签: ios objective-c uiimage uicolor tintcolor


【解决方案1】:

你可以试试:

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

【讨论】:

  • 这段代码给出了 CGContextDrawImage: invalid context 0x0。这是一个严重的错误。此应用程序或它使用的库正在使用无效的上下文,从而导致系统稳定性和可靠性的整体下降。此通知是出于礼貌:请解决此问题。这将成为即将到来的更新中的致命错误。有什么想法吗?
  • 检查您的 UIImageView 实例是否为空。如果没有针对空的 UIIMageView 调用该方法。
  • 是的,有一个图像,图像显示在图像视图中,然后在用户选择颜色后,我想改变它的颜色
  • 你可以在一些孤立的环境中尝试我建议的方法吗?就像一个空项目并对颜色和图像进行硬编码,只是为了确保错误消息与此方法无关,而是与我们看不到的其他内容相关(我们只有几行代码)。因为我尝试过并且没有任何错误消息。
  • 是的thanx,我做到了,但是这个解决方案给出了问题中提到的相同结果。为了更好地理解我的情况,假设您的 imageview 包含连衣裙的图像,我希望连衣裙的颜色根据用户选择的颜色而改变.. 有什么想法吗?
【解决方案2】:

试试这样的

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;

希望这会有所帮助。

【讨论】:

  • 感谢帮助,但这会将图像更改为白色图像(因为它是空白图像)
  • 所以你是说你的 _sketchImageView.image 是空白的?
  • 不,不是……我的意思是在应用您的代码后,我得到了一张白色图像
  • 啊,好的,抱歉。这个对我有用。我想我的图像更简单(即背景清晰的一种颜色)。
【解决方案3】:

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

【讨论】:

    【解决方案4】:

    尝试为自己生成新图像

    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

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

    SO:您的图像如何取决于您的设计方式

    【讨论】:

    • 非常感谢,假设我有一个书本图像,这个解决方案为书本边界外的图像着色..我想要相反的,即书本本身被着色。知道为什么会这样吗?
    • 我认为您需要重新设计您的图书图片。我刚刚编辑了我的帖子。看看吧
    • Yeah.yessss.. 我试过你的图片,效果很好:) 我真的很感激
    • 对我不起作用,我更喜欢这个答案:*.com/questions/19274789/…
    【解决方案5】:

    在 Swift 中你可以使用这个扩展:[基于 @VietHung 的 Objective-c 解决方案]

    斯威夫特 5:

    extension UIImage {
          func imageWithColor(color: UIColor) -> UIImage? {
            var image = 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
        }
    }
    

    以前的 Swift 版本:

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

    【讨论】:

      【解决方案6】:

      以下是我在 IOS 9 中使用 Swift 应用和使用色调的方法。

      //apply a color to an image
      //ref - http://*.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;
      }
      

      【讨论】:

        【解决方案7】:

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

        【讨论】:

          【解决方案8】:

          在 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
          

          【讨论】:

          • 对于 Swift 3.0,您需要使用 withRenderingMode,而不是 imageWithRenderingMode。
          【解决方案9】:

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

          class TintedUIImageView: UIImageView {
          
              override func awakeFromNib() {
                  if let image = self.image {
                      self.image = image.withRenderingMode(.alwaysTemplate)
                  }
              }
          }
          

          【讨论】:

          • 欢迎来到 SO!您的回复看起来像是评论而不是答案。一旦你有足够的reputation,你就可以在任何帖子上comment。还要检查这个what can I do instead。如果您打算回答,请阅读此how-to-answer 以遵循 SO 指南。
          【解决方案10】:

          您可以做的一件事是,只需将您的图像添加到 XCode 中的 Assets 文件夹,然后将渲染模式更改为 Template Image,这样每当您更改 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】:

            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】:

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

              【讨论】:

                【解决方案13】:
                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】:

                  试试这个

                  iOS 13.4 及更高版本

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

                  【讨论】: