如何着色图像/显示颜色? [英] How to tint an image/show a colour?

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

问题描述

我想要做的2件事,这些事情是相关的:

2 things i want to do, which are related:


  1. 显示任意颜色的块。所以我可以随时将其颜色更改为其他颜色。

  2. 将UIImage设为不同的颜色。阿尔法调低的颜色叠加可以在这里工作,但是说它是一个具有透明背景并且没有占据图像的整个正方形的图像。

任何想法?

推荐答案

第一个很容易。制作一个新的 UIView 并将其背景颜色设置为您想要的任何颜色。

The first one is easy. Make a new UIView and set its background color to whatever color you’d like.

第二个更难。正如您所提到的,您可以在透明度调低的情况下在其上面放置一个新视图,但是为了让它在相同位置剪辑,您需要使用遮罩。这样的事情:

The second is more difficult. As you mentioned, you can put a new view on top of it with transparency turned down, but to get it to clip in the same places, you’d want to use a mask. Something like this:

UIImage *myImage = [UIImage imageNamed:@"foo.png"];

UIImageView *originalImageView = [[UIImageView alloc] initWithImage:myImage];
[originalImageView setFrame:CGRectMake(0.0f, 0.0f, 100.0f, 100.0f)];
[parentView addSubview:originalImageView];

UIView *overlay = [[UIView alloc] initWithFrame:[originalImageView frame]];

UIImageView *maskImageView = [[UIImageView alloc] initWithImage:myImage];
[maskImageView setFrame:[overlay bounds]];

[[overlay layer] setMask:[maskImageView layer]];

[overlay setBackgroundColor:[UIColor redColor]];

[parentView addSubview:overlay];

请记住,您必须 #import< QuartzCore / QuartzCore实现文件中的.h>

这篇关于如何着色图像/显示颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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