天天看点

ios 中使用blend改变图片的颜色

在ios开发的过程中,我们经常会遇到这样的情况:当我们点击一个按钮时,按钮的背景颜色会发生变化,一般情况下 我们会调用UIButton 的函数- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state 来设置button在正常的情况下和按下时的背景颜色。     此时我们就需要两张图片,制作图片的过程很复杂。

因此我们可以通过修改图片的背景颜色达到这样的效果:

blendMode在ios中是一个枚举类型:如下所示

enum CGBlendMode {

    kCGBlendModeNormal,

    kCGBlendModeMultiply,

    kCGBlendModeScreen,

    kCGBlendModeOverlay,

    kCGBlendModeDarken,

    kCGBlendModeLighten,

    kCGBlendModeColorDodge,

    kCGBlendModeColorBurn,

    kCGBlendModeSoftLight,

    kCGBlendModeHardLight,

    kCGBlendModeDifference,

    kCGBlendModeExclusion,

    kCGBlendModeHue,

    kCGBlendModeSaturation,

    kCGBlendModeColor,

    kCGBlendModeLuminosity,

    kCGBlendModeClear,            

    kCGBlendModeCopy,            

    kCGBlendModeSourceIn,        

    kCGBlendModeSourceOut,        

    kCGBlendModeSourceAtop,        

    kCGBlendModeDestinationOver,    

    kCGBlendModeDestinationIn,        

    kCGBlendModeDestinationOut,        

    kCGBlendModeDestinationAtop,    

    kCGBlendModeXOR,            

    kCGBlendModePlusDarker,        

    kCGBlendModePlusLighter        

};

注:premultiplied result, source, and destination colors with alpha; Ra,

       Sa, and Da are the alpha components of these colors.    R:表示结果   S表示包含alpha的原色, D表示含有alpha的目标色,Ra,Sa,Da分别是3个的alpja

(一) 首先创建一个UIImage的类别:

@interface UIImage (Tint)

-(UIImage *)imageWithTintColor:(UIColor *)tintColor;   

-(UIImage *) imageWithGradientTintClolor:(UIColor *)tintColor;

@end

(二) 实现类别中的方法

//

//  UIImage + Tint.m

//  practice

//

//  Created by user on 15/3/24.

//  Copyright (c) 2015年 user. All rights reserved.

//

#import "UIImage + Tint.h"

@implementation UIImage (Tint)

-(UIImage *)imageWithTintColor:(UIColor *)tintColor blendMode: (CGBlendMode) blendMode

{

    UIGraphicsBeginImageContextWithOptions(self.size, NO,0.0f);

    [tintColor setFill];

    CGRect bounds = CGRectMake(0, 0, self.size.width, self.size.height);

    UIRectFill(bounds);

    [self drawInRect:bounds blendMode:blendMode alpha:1.0f];

    if(blendMode != kCGBlendModeDestinationIn)

    {

        [self drawInRect:bounds blendMode:kCGBlendModeDestinationIn alpha:1.0f];

    }

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return image;

}

-(UIImage *)imageWithTintColor:(UIColor *)tintColor

{

    return [self imageWithTintColor:tintColor blendMode:kCGBlendModeDestinationIn];// 保留原图的透明度

}

-(UIImage *) imageWithGradientTintClolor:(UIColor *)tintColor

{

    return [self imageWithTintColor:tintColor blendMode:kCGBlendModeOverlay];     //保持背景色的明暗,就是灰暗信息

}

@end

(三)使用类别中的方法

 UIImageView *imageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(150, 150, 34, 34)];

    UIImageView *imageView2 = [[UIImageView alloc]initWithFrame:CGRectMake(200, 200, 34, 34)];

    UIImageView *imageView3 = [[UIImageView alloc]initWithFrame:CGRectMake(250,250, 34, 34)];

    imageView1.image = [UIImage imageNamed:@"[email protected]"];

    imageView2.image = [[UIImage imageNamed:@"auditing_btn[email protected]"] imageWithTintColor:[UIColor orangeColor]];

    imageView3.image = [[UIImage imageNamed:@"auditing_btn_message_[email protected]"] imageWithGradientTintClolor:[UIColor orangeColor]];

    [_firstView addSubview:imageView1];

    [_firstView addSubview:imageView2];

    [_firstView addSubview:imageView3];

程序运行之后的效果

ios 中使用blend改变图片的颜色
ios 中使用blend改变图片的颜色

  分别是3张图片的放在视图上显示的结果