我如何在iPhone上编程图像? [英] How would I tint an image programatically on the iPhone?

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

问题描述

我想使用颜色参考来调色图像。结果应类似于Photoshop中的乘法混合模式,其中 将替换为 tint



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



我会不断更改颜色值



后续:我会把代码在ImageView的drawRect:方法中执行, b

一如往常,一个代码片段将大大有助于我的理解,而不是链接。



Update:建议将代码 Ramin 子类化为UIImageView。



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

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

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



更新:drawRect:is not

最终更新:我找到一个旧项目,其中有一个imageView设置正确,所以我可以测试Ramin的代码,它的工作原理一个魅力!



最后,最后更新



学习Core Graphics,这里是最简单的事情,可能工作。



在你的子类UIView:

   - (void)drawRect:(CGRect)rect {

CGContextRef context = UIGraphicsGetCurrentContext

CGContextSetFillColor(context,CGColorGetComponents([UIColor colorWithRed:0.5 green:0.5 blue:0 alpha:1] .CGColor)); //不要使颜色太饱和

CGContextFillRect(context,rect); // draw base

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


解决方案

将需要子类化UIImageView并覆盖drawRect方法。你的类需要一个UIColor属性(我们称之为overlayColor)来保存混合颜色和一个自定义setter,当颜色改变时强制重画。像这样的:

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

overlayColor = [newColor retain];
[self setNeedsDisplay]; // drawRect每次颜色改变
}

想要先绘制图像,然后使用合适的混合模式填充您想要的颜色的矩形,这样的东西:

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

//先绘制图片
//
CGContextDrawImage(context,self.frame,self.image.CGImage);

//混合模式可以是任何CGBlendMode值。现在绘制填充矩形
//在图像顶部。
//
CGContextSetBlendMode(context,kCGBlendModeMultiply);
CGContextSetFillColor(context,CGColorGetComponents(self.overlayColor.CGColor));
CGContextFillRect(context,self.bounds);
CGContextRestoreGState(context);
}

通常为了优化绘图, in drawRect,但是由于背景图像必须在每次颜色更改时重绘,所以很可能整个事物需要刷新。



要使用它创建一个对象然后将 image 属性(继承自UIImageView)设置为图片,并将 overlayColor 设置为UIColor值可以通过改变传递的颜色的alpha值来调整)。


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:

I will be changing the color value continuously.

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.

Update: Subclassing a UIImageView with the code Ramin suggested.

I put this in viewDidLoad: of my view controller:

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

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.

Update: drawRect: is not being called.

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!

Final, final update:

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

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
}

解决方案

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
}

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

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.

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天全站免登陆