iOS-改變矢量圖的顏色
作者:whojun
設(shè)計師能給你切全套那是最好的,自己也省事。不過嘛,這個是有好處滴,可以節(jié)省app大小,可以省去按鈕的高亮、選中等等圖片。
為啥要改變矢量圖的顏色呢,直接叫設(shè)計師切個圖不就好呢。
主要最近呢,設(shè)計師回家了,沒辦法只有一張圖,我又懶的用ps。
就給自己弄了個課題。
其實也簡單在ps中也不就是選中選區(qū),填充顏色,然后保存。
在iOS也是差不多的。
- //UIImage+ImageColor.h
- #import <UIKit/UIKit.h>
- @interface UIImage (ImageColor)
- /**
- * 修改矢量圖顏色
- *
- * @param maskColor 修改顏色
- * @return
- */
- - (UIImage *)imageMaskWithColor:(UIColor *)maskColor;
- @end
- //UIImage+ImageColor.m
- #import "UIImage+ImageColor.h"
- @implementation UIImage (ImageColor)
- - (UIImage *)imageMaskWithColor:(UIColor *)maskColor {
- if (!maskColor) {
- return nil;
- }
- UIImage *newImage = nil;
- CGRect imageRect = (CGRect){CGPointZero,self.size};
- UIGraphicsBeginImageContextWithOptions(imageRect.size, NO, self.scale);
- CGContextRef context = UIGraphicsGetCurrentContext();
- CGContextScaleCTM(context, 1.0, -1.0);
- CGContextTranslateCTM(context, 0.0, -(imageRect.size.height));
- CGContextClipToMask(context, imageRect, self.CGImage);//選中選區(qū) 獲取不透明區(qū)域路徑
- CGContextSetFillColorWithColor(context, maskColor.CGColor);//設(shè)置顏色
- CGContextFillRect(context, imageRect);//繪制
- newImage = UIGraphicsGetImageFromCurrentImageContext();//提取圖片
- UIGraphicsEndImageContext();
- return newImage;
- }
- @end
總結(jié):
設(shè)計師能給你切全套那是***的,自己也省事。
不過嘛,這個是有好處滴,可以節(jié)省app大小,可以省去按鈕的高亮、選中等等圖片。
責(zé)任編輯:倪明
來源:
簡書