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

本文介绍了一种通过Core Graphics中的Blend Mode技术,使用CGBlendMode枚举类型来改变按钮背景颜色的方法,避免了传统方法需要制作两张图片的繁琐过程。通过自定义UIImage类别,实现了按钮在不同状态下的背景色变化效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



在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,

    /* Available in Mac OS X 10.5 & later. R, S, and D are, respectively,
       premultiplied result, source, and destination colors with alpha; Ra,
       Sa, and Da are the alpha components of these colors.

       The Porter-Duff "source over" mode is called `kCGBlendModeNormal':
         R = S + D*(1 - Sa)

       Note that the Porter-Duff "XOR" mode is only titularly related to the
       classical bitmap XOR operation (which is unsupported by
       CoreGraphics). */

    kCGBlendModeClear,            /* R = 0 */
    kCGBlendModeCopy,            /* R = S */
    kCGBlendModeSourceIn,        /* R = S*Da */
    kCGBlendModeSourceOut,        /* R = S*(1 - Da) */
    kCGBlendModeSourceAtop,        /* R = S*Da + D*(1 - Sa) */
    kCGBlendModeDestinationOver,    /* R = S*(1 - Da) + D */
    kCGBlendModeDestinationIn,        /* R = D*Sa */
    kCGBlendModeDestinationOut,        /* R = D*(1 - Sa) */
    kCGBlendModeDestinationAtop,    /* R = S*(1 - Da) + D*Sa */
    kCGBlendModeXOR,            /* R = S*(1 - Da) + D*(1 - Sa) */
    kCGBlendModePlusDarker,        /* R = MAX(0, (1 - D) + (1 - S)) */
    kCGBlendModePlusLighter        /* R = MIN(1, S + D) */
};

注: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:@"auditing_btn_message_pre@2x.png"];
    
    imageView2.image = [[UIImage imageNamed:@"auditing_btn_message_pre@2x.png"] imageWithTintColor:[UIColor orangeColor]];
    
    imageView3.image = [[UIImage imageNamed:@"auditing_btn_message_pre@2x.png"] imageWithGradientTintClolor:[UIColor orangeColor]];
    
    [_firstView addSubview:imageView1];
    [_firstView addSubview:imageView2];
    [_firstView addSubview:imageView3];


程序运行之后的效果


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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值