【问题标题】:Tint color of UIImage in XIBXIB中UIImage的色调颜色
【发布时间】:2013-09-20 09:31:37
【问题描述】:

我尝试了不同的方法,但还没有找到这个问题的答案。 有没有办法给 XIB 文件中 UIImageView 定义的图像着色?

感谢

【问题讨论】:

  • 我认为不可能。
  • 问题是什么?

标签: ios uiimageview xib


【解决方案1】:

这行得通

- (UIImage *)imageWithBurnTint:(UIColor *)color { UIImage *img = self; // 让图标着色 - 假设您的图标是黑色的 UIGraphicsBeginImageContextWithOptions(img.size, NO, 0.0); CGContextRef 上下文 = UIGraphicsGetCurrentContext(); CGContextTranslateCTM(context, 0, img.size.height); CGContextScaleCTM(上下文, 1.0, -1.0); CGRect rect = CGRectMake(0, 0, img.size.width, img.size.height); // 绘制 alpha 掩码 CGContextSetBlendMode(context, kCGBlendModeNormal); CGContextDrawImage(context, rect, img.CGImage); // 绘制 tint 颜色,保留原始图像的 alpha 值 CGContextSetBlendMode(context, kCGBlendModeSourceIn); [颜色设置填充]; CGContextFillRect(context, rect); UIImage *coloredImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); 返回彩色图像; }

然后

[anImageViewInXib setImage:[center.image imageWithBurnTint:[UIColor blackColor]]];

【讨论】:

    【解决方案2】:

    您需要做的就是更改系统

    的按钮类型

    然后您将能够管理图像的色调

    结果如下图:

    【讨论】:

      【解决方案3】:

      你可以试试这个.for tint image

      @interface UIImage (Additions)
      + (UIImage *)imageWithColor:(UIColor *)color;
      
      #import "UIImage+Additions.h"
      
      @implementation UIImage (Additions)
      
      + (UIImage *)imageWithColor:(UIColor *)color {
          CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
          UIGraphicsBeginImageContext(rect.size);
          CGContextRef context = UIGraphicsGetCurrentContext();
      
          CGContextSetFillColorWithColor(context, [color CGColor]);
          CGContextFillRect(context, rect);
      
          UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
          UIGraphicsEndImageContext();
      
          return image;
      }
      

      【讨论】:

      • 试过类似的东西(你代码中的矩形不是根据实际图片大小)但不起作用。
      【解决方案4】:

      您可以使用新的 iOS7 api 来执行此操作。

              UIImage *image = [self.imageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
              imageView.image = image;
              imageView.tintColor = [UIColor redColor];
      

      第一步是获取现有图像并复制该图像并将渲染模式设置为模板。将返回的图像设置回 imageView,最后在图像视图上设置 tint 颜色。

      【讨论】:

      • 你可以直接在UIImage上使用-imageWithRenderingMode:——你不需要先把它放在一个图像视图中。
      • @MikeRhodes 是的,这是正确的。我只是在@Vanya 问题的上下文中发布代码。他试图从笔尖为图像视图中的图像着色。因此,我发布了一个代码示例,他可以输入awakeFromNib,该示例可以为他放置在笔尖中的 uiimageivew 中的任何图像着色。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-07
      • 2012-05-03
      • 2012-10-04
      • 1970-01-01
      • 1970-01-01
      • 2018-03-11
      相关资源
      最近更新 更多