iOS中使用blend改变图片颜色

使用代码改变图片的颜色需要用到blend属性

目标:

在应用里一个很常见的需求是主题变换:同样的图标,同样的素材,但是需要按照用户喜爱变为不同的颜色。在iOS5和6的SDK里部分标准控件引入了tintColor,来满足个性化界面的需求,但是Apple在这方面还远远做的不够。一是现在用默认控件根本难以做出界面优秀的应用,二是tintColor所覆盖的并不够全面,在很多情况下开发者都无法使用其来完成个性化定义。

如果有一丁点重构的意识,就会知道这不是一个很好的解决方案。工程中存在大量的冗余和重复(就算你要狡辩这些图片颜色不同不算重复,你也会在内心里知道这种狡辩是多么无力),这是非常致命的。想象一下如果你有10套主题界面,先不论应用的体积会膨胀到多少,光是想做一点修改就会痛苦万分,比如希望改一下某个按钮的形状,很好,设计师大大请重复地修改10遍,并出10套UI,然后一系列的重命名,文件移动和导入…一场灾难。

当然有其他办法,因为说白了就是tint不同的颜色到图片上而已,如果我们能实现改变UIImage的颜色,那我们就只需要一套UI,然后用代码来改变UI的颜色就可以了,生活有木有一下光明起来呀。嗯,让我们先从一张图片开始吧~下面是一张带有alpha通道的图片,原始颜色是纯的灰色

原始图片

我们将用blending给这张图片加上另一个纯色作为tint,并保持原来的alpha通道。用Core Graphics来做的话,大概的想法很直接:

1. 创建一个上下文用以画新的图片
2. 将新的tintColor设置为填充颜色
3. 将原图片画在创建的上下文中,并用新的填充色着色(注意保持alpha通道)
4. 从当前上下文中取得图片并返回

最麻烦的部分可能就是保持alpha通道了。UIImage的文档中提供了使用blend绘图的方法drawInRect:blendMode:alpha:rectalpha都没什么问题,但是blendMode是个啥玩意儿啊…继续看文档,关于CGBlendMode的文档,里面有一大堆看不懂的枚举值,比如这样:

kCGBlendModeDestinationOver  
R = S*(1 - Da) + D  
Available in iOS 2.0 and later.  
Declared in CGContext.h. 

完全不懂..直接看之后的Discussion部分:

The blend mode constants introduced in OS X v10.5 represent the Porter-Duff blend modes. The symbols in the equations for these blend modes are:
R is the premultiplied result
S is the source color, and includes alpha
D is the destination color, and includes alpha
Ra, Sa, and Da are the alpha components of R, S, and D

原来如此,R表示结果,S表示包含alpha的原色,D表示包含alpha的目标色,Ra,Sa和Da分别是三个的alpha。明白了这些以后,就可以开始寻找我们所需要的blend模式了。相信你可以和我一样,很快找到这个模式:

kCGBlendModeDestinationIn  
R = D*Sa  
Available in iOS 2.0 and later.  
Declared in CGContext.h.  

结果 = 目标色和原色透明度的加成,看起来正式所需要的。啦啦啦,还等什么呢,开始动手实现看看对不对吧~

为了以后使用方便,当然是祭出Category,先创建一个UIImage的类别:

//  UIImage+Tint.h  
#import <UIKit/UIKit.h>  
@interface UIImage (Tint)  
- (UIImage *) imageWithTintColor:(UIColor *)tintColor;  
@end

暂时先这样,当然我们也可以创建一个类方法直接完成从bundle读取图片然后加tintColor,但是很多时候并不如上面一个实例方法方便(比如想要从非bundle的地方获取图片),这个问题之后再说。那么就按照之前设想的步骤来实现吧:

//  UIImage+Tint.m  
  
#import "UIImage+Tint.h"  
  
@implementation UIImage (Tint)  
- (UIImage *) imageWithTintColor:(UIColor *)tintColor  
{  
    //We want to keep alpha, set opaque to NO; Use 0.0f for scale to use the scale factor of the device’s main screen.  
    UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0f);  
    [tintColor setFill];  
    CGRect bounds = CGRectMake(0, 0, self.size.width, self.size.height);  
    UIRectFill(bounds);  
  
    //Draw the tinted image in context  
    [self drawInRect:bounds blendMode:kCGBlendModeDestinationIn alpha:1.0f];  
  
    UIImage *tintedImage = UIGraphicsGetImageFromCurrentImageContext();  
    UIGraphicsEndImageContext();  
  
    return tintedImage;  
}  
@end 

简单明了,没有任何难点。测试之:[[UIImage imageNamed:@"image"] imageWithTintColor:[UIColor orangeColor]];得到的结果为:

测试结果

嗯…怎么说呢,虽然tintColor的颜色是变了,但是总觉得怪怪的。仔细对比一下就会发现,原来灰色图里星星和周围的灰度渐变到了橙色的图里好像都消失了:星星整个变成了橙色,周围的一圈漂亮的光晕也没有了,这是神马情况啊…这种图能交差的话那算见鬼了,会被设计和产品打死的吧。对于无渐变的纯色图的图来说直接用上面的方法是没问题的,但是现在除了Metro的大色块以外哪里无灰度渐变的设计啊…检查一下使用的blend,R = D * Sa,恍然大悟,我们虽然保留了原色的透明度,但是却把它的所有的灰度信息弄丢了。怎么办?继续刨CGBlendMode的文档吧,那么多blend模式应该总有我们需要的。功夫不负有心人,kCGBlendModeOverlay一副嗷嗷待选的样子:

kCGBlendModeOverlay  
Either multiplies or screens the source image samples with the background image samples, depending on the background color. The result is to overlay the existing image samples while preserving the highlights and shadows of the background. The background color mixes with the source image to reflect the lightness or darkness of the background.  
Available in iOS 2.0 and later.  
Declared in CGContext.h.

kCGBlendModeOverlay可以保持背景色的明暗,也就是灰度信息,听起来正是我们需要的。加入到声明中,并且添加相应的实现( 顺便重构一下原来的代码 :) ):

//  UIImage+Tint.h  
  
#import <UIKit/UIKit.h>  
  
@interface UIImage (Tint)  
  
- (UIImage *) imageWithTintColor:(UIColor *)tintColor;  
- (UIImage *) imageWithGradientTintColor:(UIColor *)tintColor;  
  
@end  
//  UIImage+Tint.m  
  
#import "UIImage+Tint.h"  
  
@implementation UIImage (Tint)  
- (UIImage *) imageWithTintColor:(UIColor *)tintColor  
{  
    return [self imageWithTintColor:tintColor blendMode:kCGBlendModeDestinationIn];  
}  
  
- (UIImage *) imageWithGradientTintColor:(UIColor *)tintColor  
{  
    return [self imageWithTintColor:tintColor blendMode:kCGBlendModeOverlay];  
}  
  
- (UIImage *) imageWithTintColor:(UIColor *)tintColor blendMode:(CGBlendMode)blendMode  
{  
    //We want to keep alpha, set opaque to NO; Use 0.0f for scale to use the scale factor of the device’s main screen.  
    UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0f);  
    [tintColor setFill];  
    CGRect bounds = CGRectMake(0, 0, self.size.width, self.size.height);  
    UIRectFill(bounds);  
  
    //Draw the tinted image in context  
    [self drawInRect:bounds blendMode:blendMode alpha:1.0f];  
  
    UIImage *tintedImage = UIGraphicsGetImageFromCurrentImageContext();  
    UIGraphicsEndImageContext();  
  
    return tintedImage;  
}  
  
@end 

完成,测试之…好吧,好尴尬,虽然颜色和周围的光这次对了,但是透明度又没了啊魂淡..一点不奇怪啊,因为kCGBlendModeOverlay本来就没承诺给你保留原图的透明度的说。


没有透明度

那么..既然我们用kCGBlendModeOverlay能保留灰度信息,用kCGBlendModeDestinationIn能保留透明度信息,那就两个blendMode都用不就完事儿了么~尝试之,如果在blend绘图时不是kCGBlendModeDestinationIn模式的话,则再用kCGBlendModeDestinationIn画一次:

//  UIImage+Tint.m  
  
#import "UIImage+Tint.h"  
  
@implementation UIImage (Tint)  
- (UIImage *) imageWithTintColor:(UIColor *)tintColor  
{  
    return [self imageWithTintColor:tintColor blendMode:kCGBlendModeDestinationIn];  
}  
  
- (UIImage *) imageWithGradientTintColor:(UIColor *)tintColor  
{  
    return [self imageWithTintColor:tintColor blendMode:kCGBlendModeOverlay];  
}  
  
- (UIImage *) imageWithTintColor:(UIColor *)tintColor blendMode:(CGBlendMode)blendMode  
{  
    //We want to keep alpha, set opaque to NO; Use 0.0f for scale to use the scale factor of the device’s main screen.  
    UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0f);  
    [tintColor setFill];  
    CGRect bounds = CGRectMake(0, 0, self.size.width, self.size.height);  
    UIRectFill(bounds);  
  
    //Draw the tinted image in context  
    [self drawInRect:bounds blendMode:blendMode alpha:1.0f];  
  
    if (blendMode != kCGBlendModeDestinationIn) {  
        [self drawInRect:bounds blendMode:kCGBlendModeDestinationIn alpha:1.0f];  
    }  
  
    UIImage *tintedImage = UIGraphicsGetImageFromCurrentImageContext();  
    UIGraphicsEndImageContext();  
  
    return tintedImage;  
}  
  
@end 

结果如下:


结果图片

已经很完美了,这样的话只要在代码里设定一下颜色,我们就能够很轻易地使用同样一套UI,将其blend为需要的颜色,来实现素材的重用了。唯一需要注意的是,因为每次使用UIImage+Tint的方法绘图时,都使用了CG的绘制方法,这就意味着每次调用都会是用到CPU的Offscreen drawing,大量使用的话可能导致性能的问题(主要对于iPhone 3GS或之前的设备,可能同时处理大量这样的绘制调用的能力会有不足)。关于CA和CG的性能的问题,打算在之后用一篇文章来介绍一下。对于这里的UIImage+Tint的实现,可以写一套缓存的机制,来确保大量重复的元素只在load的时候blend一次,之后将其缓存在内存中以快速读取。当然这是一个权衡的问题,在时间和空间中做出正确的平衡和选择,也正是程序设计的乐趣所在。

这篇文章中作为示例的工程和UIImage+Tint可以在Github上找到
转自:onevcat的博客

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 196,165评论 5 462
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 82,503评论 2 373
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 143,295评论 0 325
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,589评论 1 267
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,439评论 5 358
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,342评论 1 273
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,749评论 3 387
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,397评论 0 255
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,700评论 1 295
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,740评论 2 313
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,523评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,364评论 3 314
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,755评论 3 300
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,024评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,297评论 1 251
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,721评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,918评论 2 336

推荐阅读更多精彩内容