美文网首页
组件化项目问题 加载本地Bundle,图片

组件化项目问题 加载本地Bundle,图片

作者: 物非0人非 | 来源:发表于2022-05-28 21:43 被阅读0次

调用:

sureBtn.setImage(UIImage.mine_imageNamed("ic_smile"), for: UIControl.State.normal)
//
//  UIImage+Mine.h
//  AFNetworking
//
//  Created by chenweichang on 2021/8/17.
//

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface UIImage (Mine)

+ (UIImage *)mine_imageNamed:(NSString *)name;

+ (UIImage *)mine_resizedImageNamed:(NSString *)name;

+ (UIImage *)mine_animatedGIFNamed:(NSString *)name;

+ (UIImage *)mine_SVGImageNamed:(NSString *)name;

+ (UIImage *)mine_SVGImageNamed:(NSString *)name withColor:(UIColor *)color;

+ (UIImage *)mine_SVGImageNamed:(NSString *)name withSize:(CGSize)size;
+ (UIImage *)mine_SVGImageNamed:(NSString *)name withSize:(CGSize)size withColor:(UIColor *)color;

@end

NS_ASSUME_NONNULL_END
//  UIImage+Mine.m
//  AFNetworking
//
//  Created by chenweichang on 2021/8/17.
//

#import "UIImage+Mine.h"
#import "MMDelegateManager.h"
#import <XHBUIKit/XHBSVGImageView.h>

@implementation UIImage (Mine)

+ (UIImage *)mine_imageNamed:(NSString *)name {
    UIImage *image = [UIImage imageNamed:name inBundle:MineAssetsBundle compatibleWithTraitCollection:nil];
    if (!image) {
        image = [UIImage imageNamed:name inBundle:MineModuleBundle compatibleWithTraitCollection:nil];
    }
    if (!image) {
        image = [UIImage imageNamed:name];
    }
    return image;
}

+ (UIImage *)mine_resizedImageNamed:(NSString *)name {
    UIImage *image = [UIImage mine_imageNamed:name];
    return [image stretchableImageWithLeftCapWidth:image.size.width * 0.5 topCapHeight:image.size.height * 0.5];
}

+ (UIImage *)mine_animatedGIFNamed:(NSString *)name {
    if (!name) {
        return nil;
    }
    CGFloat scale = [UIScreen mainScreen].scale;
    if (scale > 1.0f) {
        NSString *retinaPath = [MineModuleBundle pathForResource:[name stringByAppendingString:@"@2x"] ofType:@"gif"];
        NSData *data = [NSData dataWithContentsOfFile:retinaPath];
        if (data) {
            return [UIImage hb_imageWithGIFData:data];
        }
        NSString *path = [MineModuleBundle pathForResource:name ofType:@"gif"];
        data = [NSData dataWithContentsOfFile:path];
        if (data) {
            return [UIImage hb_imageWithGIFData:data];
        }
        path = [[NSBundle mainBundle] pathForResource:name ofType:@"gif"];
        data = [NSData dataWithContentsOfFile:path];
        if (data) {
            return [UIImage hb_imageWithGIFData:data];
        }
        return [UIImage imageNamed:name];
        
    } else {
        NSString *path = [MineModuleBundle pathForResource:name ofType:@"gif"];
        NSData *data = [NSData dataWithContentsOfFile:path];
        if (data) {
            return [UIImage hb_imageWithGIFData:data];
        }
        return [UIImage imageNamed:name];
    }
}
+ (UIImage *)mine_SVGImageNamed:(NSString *)name {
    if ([NSString isEmpty:name]) {
        return nil;
    }
    NSBundle *bundle = MineModuleBundle;
    NSString *path = [bundle pathForResource:name ofType:@"svg"];
    if ([NSString isEmpty:path]) {
        DDLogError(@"hb_setSVGImageWithName path 为空~~~");
        return nil;
    }
    
    return [SVGKImage imageNamed:name inBundle:bundle].UIImage ;
}


+ (UIImage *)mine_SVGImageNamed:(NSString *)name withColor:(UIColor *)color {
    if ([NSString isEmpty:name]) {
        return nil;
    }
    NSBundle *bundle = MineModuleBundle;
    NSString *path = [bundle pathForResource:name ofType:@"svg"];
    if ([NSString isEmpty:path]) {
        DDLogError(@"hb_setSVGImageWithName path 为空~~~");
        return nil;
    }
    SVGKImage *svgImage = [SVGKImage imageNamed:name inBundle:bundle];
    for (CAShapeLayer *layer in svgImage.CALayerTree.sublayers) {
        if([layer isKindOfClass:CAShapeLayer.class]){
            
            layer.fillColor = color.CGColor;
        }
    }
    return svgImage.UIImage ;
}
+ (UIImage *)mine_SVGImageNamed:(NSString *)name withSize:(CGSize)size{
    return [self mine_SVGImageNamed:name withSize:size withColor:nil];
}
+ (UIImage *)mine_SVGImageNamed:(NSString *)name withSize:(CGSize)size withColor:(UIColor *)color {
    if ([NSString isEmpty:name]) {
        return nil;
    }
    NSBundle *bundle = MineModuleBundle;
    NSString *path = [bundle pathForResource:name ofType:@"svg"];
    if ([NSString isEmpty:path]) {
        DDLogError(@"hb_setSVGImageWithName path 为空~~~");
        return nil;
    }
    SVGKImage *svgImage = [SVGKImage imageNamed:name inBundle:bundle];
    
    if (color) {
        for (CAShapeLayer *layer in svgImage.CALayerTree.sublayers) {
            if([layer isKindOfClass:CAShapeLayer.class]){
                
                layer.fillColor = color.CGColor;
            }
        }
    }
    if (!CGSizeEqualToSize(size, CGSizeZero)) {
        svgImage.size = size;
    }
    return svgImage.UIImage ;
}

@end

//
//  MMDelegateManager.h
//  AFNetworking
//
//  Created by chenweichang on 2021/8/17.
//

#import <Foundation/Foundation.h>

#define MineModuleBundle [MMDelegateManager sharedInstance].mineBundle
#define MineAssetsBundle [MMDelegateManager sharedInstance].mineAssets

NS_ASSUME_NONNULL_BEGIN

@interface MMDelegateManager : NSObject<HBDelegate>

//资源
@property (nonatomic, strong) NSBundle *mineBundle;
//Assets资源
@property (nonatomic, strong) NSBundle *mineAssets;

@end

NS_ASSUME_NONNULL_END
//
//  MMDelegateManager.m
//  AFNetworking
//
//  Created by chenweichang on 2021/8/17.
//

#import "MMDelegateManager.h"
//#import "XHBIAPHelper+PPCoin.h"
//#import "MMRootMineManager.h"

@interface MMDelegateManager()


@end

@implementation MMDelegateManager

+ (MMDelegateManager *)sharedInstance {
    static id sharedInstance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[[self class] alloc] init];
    });
    return sharedInstance;
}

- (instancetype)init {
//MineModule 模块名
    if (self = [super init]) {
        NSBundle *bundle = [NSBundle bundleForClass:[self class]];
        NSURL *url = [[bundle resourceURL] URLByAppendingPathComponent:@"MineModule.bundle"];
        self.mineBundle = [NSBundle bundleWithURL:url];
        
        url = [[bundle resourceURL] URLByAppendingPathComponent:@"MineAssets.bundle"];
        self.mineAssets = [NSBundle bundleWithURL:url];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveAccountChangedNotifaction) name:kXHBAccountDidChangeNotification object:nil];
    }
    return self;
}

- (void)didReceiveAccountChangedNotifaction{
    if([AppUserManager isLogin]){
//        [MMRootMineManager requestMineInfoUser:^{
//        }];
//        [MMRootMineManager requestGetAccountInfoByUserV2:^(MMUserInfoModel * _Nonnull item) {
//
//        }];
    }
}

#pragma mark - UIApplicationDelegate's methods

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // 提前获取一下iap的列表
//    [[XHBIAPHelper sharedInstance] refreshProductList:^(NSMutableArray<MMPPCoinProductItem *> * _Nullable visibleProductList) {
//        [[XHBIAPHelper sharedInstance] prepareProductListDataFromAppStore];
//    }];
    
    return YES;
}

@end

相关文章

网友评论

      本文标题:组件化项目问题 加载本地Bundle,图片

      本文链接:https://www.haomeiwen.com/subject/hsuxertx.html