Skip to content

Instantly share code, notes, and snippets.

@manajay
Created February 11, 2019 12:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manajay/5653e7debabad2f527728a3711ec1833 to your computer and use it in GitHub Desktop.
Save manajay/5653e7debabad2f527728a3711ec1833 to your computer and use it in GitHub Desktop.
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