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

查看:130
本文介绍了如何在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天全站免登陆