iOS改变图片的颜色

栏目: IOS · 发布时间: 6年前

内容简介:在做项目的时候碰到了一个需求,就是设计做的图片的颜色和需求不符合,需要把图片的颜色改变一下。下面是改变图片颜色的方法。首先我们创建一个UIImage的Category,名为ChangeColor。这个名为ChangeColor的Category只有一个方法,即使用方法:

在做项目的时候碰到了一个需求,就是设计做的图片的颜色和需求不符合,需要把图片的颜色改变一下。下面是改变图片颜色的方法。

首先我们创建一个UIImage的Category,名为ChangeColor。这个名为ChangeColor的Category只有一个方法,即 -(UIImage*)imageChangeColor:(UIColor*)color 。这个方法的具体实现如下:

#import "UIImage+ChangeColor.h"@implementation UIImage (ChangeColor)//绘图-(UIImage*)imageChangeColor:(UIColor*)color
{    //获取画布
    UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0f);    //画笔沾取颜色
    [color setFill];    
    CGRect bounds = CGRectMake(0, 0, self.size.width, self.size.height);    UIRectFill(bounds);    //绘制一次
    [self drawInRect:bounds blendMode:kCGBlendModeOverlay alpha:1.0f];    //再绘制一次
    [self drawInRect:bounds blendMode:kCGBlendModeDestinationIn alpha:1.0f];    //获取图片
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();    UIGraphicsEndImageContext();    return img;
}@end

使用方法:

首先我们看一下不改变颜色时,真实的图片颜色

UIImage *image = [UIImage imageNamed:@"qr-code"];UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[imageView setFrame:CGRectMake(100, 100, 100, 100)];
[self.view addSubview:imageView];

得到的图片:

iOS改变图片的颜色

然后我们改变图片的颜色为绿色:

UIImage *image = [UIImage imageNamed:@"qr-code"];UIImageView *imageView = [[UIImageView alloc] initWithImage:[image imageChangeColor:[UIColor blueColor]]];
[imageView setFrame:CGRectMake(100, 100, 100, 100)];
[self.view addSubview:imageView];

运行结果:

iOS改变图片的颜色

作者:雪山飞狐_91ae

链接:https://www.jianshu.com/p/10047407463c


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Approximation Algorithms

Approximation Algorithms

Vijay V. Vazirani / Springer / 2001-07-02 / USD 54.95

'This book covers the dominant theoretical approaches to the approximate solution of hard combinatorial optimization and enumeration problems. It contains elegant combinatorial theory, useful and inte......一起来看看 《Approximation Algorithms》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试