동적 변경 TabBar

1401 단어
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/[email protected]" @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

좋은 웹페이지 즐겨찾기