标题一:创建使用分类,用 UIGraphics方法。
首先我们创建一个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 imageChangeColor:[UIColor blueColor]]];
[imageView setFrame:CGRectMake(100, 100, 100, 100)];
[self.view addSubview:imageView];
标题二:对图片进行渲染,使用系统方法。
//看下面例子,首先对图片进行渲染,其次设置imageView的tintColor即可,两句代码即可轻轻松松改变图片颜色,是不是很简单,你学会了么?
self.areaIcon.image = [[UIImage imageNamed:@“ico_title_city.png”] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
self.areaIcon.tintColor = [UIColor whiteColor];