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

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

问题描述

我想用颜色参考着色图像。结果应该类似于Photoshop中的乘法混合模式,其中白色将替换为 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:

< img src =https://i.stack.imgur.com/CqiUz.pngalt =alt text>

我将不断更改颜色值。

跟进:我会把代码放在我的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)来保持混合颜色和一个自定义的setter,它会在颜色变化时强制重绘。这样的事情:

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.

要使用它,请创建一个实例对象然后将图像属性(继承自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).

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

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