Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
custom pod loadBundleImage
//
// LSBundleUtil.m
// Pods
//
// Created by ljtwan on 2018/10/9.
//
#import "LSBundleUtil.h"
@implementation LSBundleUtil
+ (NSBundle *)getBundleWithFrameworkName:(NSString *)frameworkName bundleName:(NSString *)bundleName {
NSString *tmpBundleName = [bundleName copy];
if (![bundleName hasSuffix:@".bundle"]) {
tmpBundleName = [NSString stringWithFormat:@"%@.bundle", tmpBundleName];
}
NSString *mainBundlePath = [[NSBundle mainBundle] resourcePath];
NSString *bundlePath = [mainBundlePath stringByAppendingPathComponent:tmpBundleName];
NSBundle *bundle = [NSBundle bundleWithPath:bundlePath];
if (bundle) {
return bundle;
}
NSString *tempFramework = [frameworkName copy];
NSString *frameExtension = @".framework";
if (![tempFramework hasSuffix:frameExtension]) {
tempFramework = [tempFramework stringByAppendingString:frameExtension];
}
NSString *path = [[[NSBundle mainBundle] privateFrameworksPath] stringByAppendingPathComponent:tempFramework];
bundle = [NSBundle bundleWithPath:[path stringByAppendingPathComponent:tmpBundleName]];
if (bundle) {
return bundle;
}
return [NSBundle mainBundle];
}
+ (UIImage *)loadBundleImageNamed:(NSString *)name class:(Class)class bundleName:(NSString *)bundleName {
NSString *tmpBundleName = [bundleName copy];
if (![bundleName hasSuffix:@".bundle"]) {
tmpBundleName = [NSString stringWithFormat:@"%@.bundle", tmpBundleName];
}
NSString *appendingPath = [tmpBundleName hasPrefix:@"/"] ? tmpBundleName : [@"/" stringByAppendingString:tmpBundleName];
return [self loadBundleImageNamed:name class:class appendingPath:appendingPath];
}
+ (UIImage *)loadBundleImageNamed:(NSString *)name class:(Class)class appendingPath:(NSString *)appendingPath {
if (![appendingPath containsString:@".bundle"]) {
[appendingPath stringByAppendingString:@".bundle"];
}
NSString *bundlePath = [[NSBundle bundleForClass:class].resourcePath
stringByAppendingPathComponent:appendingPath];
NSBundle *resource_bundle = [NSBundle bundleWithPath:bundlePath];
UIImage *image = [UIImage imageNamed:name inBundle:resource_bundle compatibleWithTraitCollection:nil];
return image;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.