object c 一个改变图标颜色的方法

//AppDelegate.h
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
复制代码
//AppDelegate.m
#import "AppDelegate.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application {
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
}
- (void)applicationWillTerminate:(UIApplication *)application {
}
@end
复制代码
//ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
复制代码

//ViewController.m
#import "ViewController.h"
#import "UIImage(TintColor).h"

@interface ViewController ()
//@property (nonatomic,strong) UIImage *image;
@property (nonatomic,strong) UIImageView *imageView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    CGRect screen = [[UIScreen mainScreen]bounds];
    CGFloat oldImageViewWidth = 50;
    CGFloat oldImageViewHeigth = 50;
    CGFloat oldViewTopSpacing = 200;
    UIImageView *oldImageView = [[UIImageView alloc]initWithFrame:CGRectMake((screen.size.width - oldImageViewWidth)/2, oldViewTopSpacing, oldImageViewWidth, oldImageViewHeigth)];
    UIImage *oimage = [UIImage imageNamed:@"ic_often_courier"];
    [oldImageView setImage:oimage];
    [self.view addSubview:oldImageView];
    
    CGFloat newImageViewWidth = 50;
    CGFloat newImageViewHeigth = 50;
    CGFloat newViewTopSpacing = 400;
    UIImageView *newImageView = [[UIImageView alloc]initWithFrame:CGRectMake((screen.size.width - newImageViewWidth)/2, newViewTopSpacing, newImageViewWidth, newImageViewHeigth)];
    UIImage *nImage = [UIImage imageNamed:@"ic_often_courier"];
    UIImage *newImage = [nImage imageWithTintColor:[UIColor greenColor]];
    [newImageView setImage:newImage];
    [self.view addSubview:newImageView];
}
@end
复制代码
//UIImage(TintColor).h
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface UIImage(TintColor)
- (UIImage *)imageWithTintColor:(UIColor *)tintColor;
@end
复制代码
//UIImage(TintColor).m
#import "UIImage(TintColor).h"

@implementation UIImage(TintColor)

- (UIImage *)imageWithTintColor:(UIColor *)tintColor {
    ///UIGraphicsBeginImageContextWithOptions()创建一个临时渲染上下文,在这上面绘制原始图片。第一个参数,size,是缩放图片的尺寸,第二个参数,isOpaque 是用来决定透明通道是否被渲染。对没有透明度的图片设置这个参数为NO,可能导致图片有粉红色调。第三个参数scale是显示缩放系数。当设置成0.0,主屏幕的缩放系数将被使用,对视网膜屏显示是2.0或者更高(在iPhone6 Plus 上是 3.0)
    ///UIScreen对象定义了基于硬件显示的相关属性,用这个类来获取每一个设备显示屏幕的对象
    ///UIScreen.mainScreen.scale:计算屏幕分辨率
    UIGraphicsBeginImageContextWithOptions(self.size, NO, UIScreen.mainScreen.scale);
    ///一个不透明类型的Quartz 2D绘画环境,相当于一个画布,你可以在上面任意绘画
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGRect area = {0, 0, self.size};
    
    CGContextScaleCTM(context, 1.0, -1.0);///坐标系X,Y缩放
    
    CGContextTranslateCTM(context, 0, -area.size.height);///坐标系平移
    
    CGContextSaveGState(context);///压栈操作,保存一份当前图形上下文
    ///CGImage:是用来重绘图形,它们在应用时是按照图像的像素矩阵来绘制图片的
    ///CGContextClipToMask:裁剪的区域,第一个参数表示context的指针,第二个参数表示裁剪到context的区域,也就是mask图片映射到context的区域,第三个参数表示mask的图片,对于裁剪区域Rect中的点是否变化取决于mask图中的alpha值,若alpha为0,则对应裁剪Rect中的点为透明,若alpha为1,则对应裁剪Rect中的点无变化
    CGContextClipToMask(context, area, self.CGImage);
    
    [tintColor set];
    
    CGContextFillRect(context, area);///  填充指定的矩形
    
    CGContextRestoreGState(context);///在没有保存之前,用这个函数还原blend mode.
    
    CGContextSetBlendMode(context, kCGBlendModeDestinationOver);///设置blend mode.
    ///CGContextDrawImage允许在制定的尺寸和位置上画图,允许在特定边缘或者适应一组图片特征比如faces,裁剪图片。
    CGContextDrawImage(context, area, self.CGImage);
    ///从当前环境当中得到重绘的图片
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();///关闭当前环境
    ///设置图片的渲染模式:始终绘制图片原始状态
    return [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}

@end
复制代码

转载于:https://juejin.im/post/5cb467875188251b117f86bc

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值