Created
February 11, 2019 12:52
-
-
Save manajay/5653e7debabad2f527728a3711ec1833 to your computer and use it in GitHub Desktop.
custom pod loadBundleImage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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