UIImage+TabbarIcon.h
@interface UIImage (TabbarIcon)
/**
BarItem.image
@param imageUrl 图片链接
@return image
-------------------------------------
nav.tabBarItem.image = [UIImage imageWithData:data];方式赋值,Xcode会把图片当成@1x,即图片多大tabBarItem就多大。
所以,将图片存在本地,命名为@2x或@3x,imageWithContentsOfFile:方式进行赋值
icon链接格式,例如:@"https://gitee.com/Ccfax/HunterPrivateImages/raw/master/navi_study@2x.png"最后以@2x,或@3x结尾。
*/
+ (UIImage *)barItemImage:(NSString *)imageUrl;
@end
UIImage+TabbarIcon.m
#import "UIImage+TabbarIcon.h"
@implementation UIImage (TabbarIcon)
+ (UIImage *)barItemImage:(NSString *)imageUrl{
NSArray *stringArr = [imageUrl componentsSeparatedByString:@"/"];
NSString *imageName = stringArr.lastObject;
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageUrl]];
UIImage *image = [UIImage imageWithData:data];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES);
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:imageName];
// 判断文件是否已存在,存在,删除原文件
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
[[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];
}
// 将image写入沙河
[UIImagePNGRepresentation(image) writeToFile:filePath atomically:YES];
return [UIImage imageWithContentsOfFile:filePath];
}
@end