废话不多说,直接上菜
1、Pod导入SVGKit
在项目里的Podfile添加pod 'SVGKit'
打开Mac终端,cd(这里有个空格)
项目的根目录,回车,输入pod install
,再回车,等……好了,打开项目一看,成功导入。
2、简单封装
写一个UIImage
的扩展:UIImage+SVGKit
里面有导入SVGKit的头文件#import "SVGKImage.h"
- .h文件
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface UIImage (SVGKit)
/// 单纯加载svg图片
/// @param name 图片名
/// @param imgv 显示的UIImageView
+(UIImage *)svgImageNamed:(NSString *)name imgv:(UIImageView *)imgv;
/// 加载svg图片,用16进制色值修改颜色
/// @param name 图片名
/// @param imgv 显示的UIImageView
/// @param hexColor 16进制色值
+(UIImage *)svgImageNamed:(NSString *)name imgv:(UIImageView *)imgv hexColor:(NSString *)hexColor;
/// 加载svg图片,用色值对象修改颜色
/// @param name 图片名
/// @param imgv 显示的UIImageView
/// @param objColor 色值对象
+(UIImage *)svgImageNamed:(NSString *)name imgv:(UIImageView *)imgv objColor:(UIColor *)objColor;
@end
NS_ASSUME_NONNULL_END
- .m文件
#import "UIImage+SVGKit.h"
#import "SVGKImage.h"
@implementation UIImage (SVGKit)
+(UIImage *)svgImageNamed:(NSString *)name imgv:(UIImageView *)imgv{
return [UIImage name:name imgv:imgv];
}
+(UIImage *)svgImageNamed:(NSString *)name imgv:(UIImageView *)imgv hexColor:(NSString *)hexColor{
UIColor *tintColor = [UIImage colorWithHexString:hexColor];
return [UIImage name:name imgv:imgv tintColor:tintColor];
}
+(UIImage *)svgImageNamed:(NSString *)name imgv:(UIImageView *)imgv objColor:(UIColor *)objColor{
UIColor *tintColor = objColor;
return [UIImage name:name imgv:imgv tintColor:tintColor];
}
#pragma mark - 加载svg图片统一调用此方法
+(UIImage *)name:(NSString *)name imgv:(UIImageView *)imgv{
SVGKImage *svgImage = [SVGKImage imageNamed:name];
svgImage.size = CGSizeMake(imgv.frame.size.width, imgv.frame.size.height);
return svgImage.UIImage;
}
#pragma mark - 修改颜色统一调用此方法
+(UIImage *)name:(NSString *)name imgv:(UIImageView *)imgv tintColor:(UIColor *)tintColor{
UIImage *svgImage = [UIImage name:name imgv:imgv];
CGRect rect = CGRectMake(0, 0, svgImage.size.width, svgImage.size.height);
CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(svgImage.CGImage);
BOOL opaque = alphaInfo == (kCGImageAlphaNoneSkipLast | kCGImageAlphaNoneSkipFirst | kCGImageAlphaNone);
UIGraphicsBeginImageContextWithOptions(svgImage.size, opaque, svgImage.scale);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, 0, svgImage.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextSetBlendMode(context, kCGBlendModeNormal);
CGContextClipToMask(context, rect, svgImage.CGImage);
CGContextSetFillColorWithColor(context, tintColor.CGColor);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
#pragma mark - 16进制色值转化
+(UIColor *)colorWithHexString:(NSString *)color{
NSString *cString = [[color stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
// String should be 6 or 8 characters
if ([cString length] < 6) {
return [UIColor clearColor];
}
// 判断前缀
if ([cString hasPrefix:@"0X"])
cString = [cString substringFromIndex:2];
if ([cString hasPrefix:@"#"])
cString = [cString substringFromIndex:1];
if ([cString length] != 6)
return [UIColor clearColor];
// 从六位数值中找到RGB对应的位数并转换
NSRange range;
range.location = 0;
range.length = 2;
//R、G、B
NSString *rString = [cString substringWithRange:range];
range.location = 2;
NSString *gString = [cString substringWithRange:range];
range.location = 4;
NSString *bString = [cString substringWithRange:range];
// Scan values
unsigned int r, g, b;
[[NSScanner scannerWithString:rString] scanHexInt:&r];
[[NSScanner scannerWithString:gString] scanHexInt:&g];
[[NSScanner scannerWithString:bString] scanHexInt:&b];
return [UIColor colorWithRed:((float) r / 255.0f)
green:((float) g / 255.0f)
blue:((float) b / 255.0f)
alpha:1.0f];
}
@end
3、调用
导入头文件#import "UIImage+SVGKit.h"
简单写一个加载图片的UIImageView
,并调用扩展如下:
UIImageView *ivCover = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
ivCover.center = self.view.center;
[self.view addSubview:ivCover];
ivCover.backgroundColor = UIColor.groupTableViewBackgroundColor;
// ivCover.image = [UIImage svgImageNamed:@"icon_new" imgv:ivCover];
// ivCover.image = [UIImage svgImageNamed:@"icon_new" imgv:ivCover hexColor:@"#ff0000"];
ivCover.image = [UIImage svgImageNamed:@"icon_new" imgv:ivCover objColor:UIColor.purpleColor];