如何在 iOS 上以编程方式为图像着色? [英] How would I tint an image programmatically on iOS?

查看:17
本文介绍了如何在 iOS 上以编程方式为图像着色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用颜色参考为图像着色.结果应该类似于 Photoshop 中的乘法混合模式,其中 whites 将替换为 tint:

I would like to tint an image with a color reference. The results should look like the Multiply blending mode in Photoshop, where whites would be replaced with tint:

我会不断改变颜色值.

跟进:我会将代码放在我的 ImageView 的 drawRect: 方法中,对吗?

Follow up: I would put the code to do this in my ImageView's drawRect: method, right?

与往常一样,代码片段对我的理解有很大帮助,而不是链接.

As always, a code snippet would greatly aid in my understanding, as opposed to a link.

更新:使用 Ramin 建议的代码对 UIImageView 进行子类化.

Update: Subclassing a UIImageView with the code Ramin suggested.

我把它放在视图控制器的 viewDidLoad: 中:

I put this in viewDidLoad: of my view controller:

[self.lena setImage:[UIImage imageNamed:kImageName]];
[self.lena setOverlayColor:[UIColor blueColor]];
[super viewDidLoad];

我看到了图像,但它没有被染色.我还尝试加载其他图像,在 IB 中设置图像,并在我的视图控制器中调用 setNeedsDisplay:.

I see the image, but it is not being tinted. I also tried loading other images, setting the image in IB, and calling setNeedsDisplay: in my view controller.

更新:drawRect:没有被调用.

Update: drawRect: is not being called.

最终更新:我发现了一个旧项目,它的 imageView 设置正确,所以我可以测试 Ramin 的代码,它的效果非常好!

Final update: I found an old project that had an imageView set up properly so I could test Ramin's code and it works like a charm!

最后的最后更新:

对于那些刚刚学习 Core Graphics 的人,这里是最简单的方法.

For those of you just learning about Core Graphics, here is the simplest thing that could possibly work.

在你的子类 UIView 中:

In your subclassed UIView:

- (void)drawRect:(CGRect)rect {

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetFillColor(context, CGColorGetComponents([UIColor colorWithRed:0.5 green:0.5 blue:0 alpha:1].CGColor)); // don't make color too saturated

    CGContextFillRect(context, rect); // draw base

    [[UIImage imageNamed:@"someImage.png"] drawInRect: rect blendMode:kCGBlendModeOverlay alpha:1.0]; // draw image
}

推荐答案

首先,您需要继承 UIImageView 并覆盖 drawRect 方法.您的类需要一个 UIColor 属性(我们称之为overlayColor)来保存混合颜色和一个在颜色改变时强制重绘的自定义设置器.像这样:

First you'll want to subclass UIImageView and override the drawRect method. Your class needs a UIColor property (let's call it overlayColor) to hold the blend color and a custom setter that forces a redraw when the color changes. Something like this:

- (void) setOverlayColor:(UIColor *)newColor {
   if (overlayColor)
     [overlayColor release];

   overlayColor = [newColor retain];
   [self setNeedsDisplay]; // fires off drawRect each time color changes
}

在 drawRect 方法中,您需要先绘制图像,然后用一个矩形覆盖它,该矩形填充了您想要的颜色以及适当的混合模式,如下所示:

In the drawRect method you'll want to draw the image first then overlay it with a rectangle filled with the color you want along with the proper blending mode, something like this:

- (void) drawRect:(CGRect)area
{
  CGContextRef context = UIGraphicsGetCurrentContext();
  CGContextSaveGState(context);

  // Draw picture first
  //
  CGContextDrawImage(context, self.frame, self.image.CGImage);

  // Blend mode could be any of CGBlendMode values. Now draw filled rectangle
  // over top of image.
  //
  CGContextSetBlendMode (context, kCGBlendModeMultiply);
  CGContextSetFillColor(context, CGColorGetComponents(self.overlayColor.CGColor));      
  CGContextFillRect (context, self.bounds);
  CGContextRestoreGState(context);
}

通常要优化绘图,您会将实际绘图限制为仅传递给 drawRect 的区域,但由于每次颜色更改时都必须重新绘制背景图像,因此整个内容可能需要刷新.

Ordinarily to optimize the drawing you would restrict the actual drawing to only the area passed in to drawRect, but since the background image has to be redrawn each time the color changes it's likely the whole thing will need refreshing.

要使用它创建对象的实例,然后将 image 属性(从 UIImageView 继承)设置为图片并将 overlayColor 设置为 UIColor 值(混合级别可以可以通过更改传递的颜色的 alpha 值来调整).

To use it create an instance of the object then set the image property (inherited from UIImageView) to the picture and overlayColor to a UIColor value (the blend levels can be adjusted by changing the alpha value of the color you pass down).

这篇关于如何在 iOS 上以编程方式为图像着色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆