1. 3.加载图片资源
NSBundle+Resource.h
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface NSBundle (Resource)
+ (instancetype)resourceBundleWithClassName:(NSString *)className
bundleName:(NSString *)bundleName;
+ (instancetype)resourceBundleWithBundleName:(NSString *)bundleName;
+ (instancetype)resourceBundleWithFramework:(NSString *)frameworkName
bundleName:(NSString *)bundleName;
+ (instancetype)getFrameworkBundleWithClassName:(NSString *)className;
+ (instancetype)getFrameworkBundleWithFramework:(NSString *)frameworkName;
+ (instancetype)getMainBundleWithResourceBundle:(NSString *)bundleName;
@end
NS_ASSUME_NONNULL_END
NSBundle+Resource.m
#import "NSBundle+Resource.h"
@implementation NSBundle (Resource)
+ (instancetype)resourceBundleWithClassName:(NSString *)className
bundleName:(NSString *)bundleName{
if (!className) {
NSAssert(!className, @"获取资源路径bundle时,类名为空");
return nil;
}
NSBundle *frameworkBundle = [NSBundle bundleForClass:NSClassFromString(className)];
if (!frameworkBundle) {
NSAssert(!className, @"获取资源路径bundle时,获取framework的bundle为空");
return nil;
}
NSURL *frameworkBundleUrl = [frameworkBundle URLForResource:bundleName withExtension:@"bundle"];
if (!frameworkBundleUrl) {
NSAssert(!className, @"获取资源路径bundle时,获取frameworkbundleURL为空");
return nil;
}
return [self bundleWithURL:frameworkBundleUrl];
}
+ (instancetype)resourceBundleWithBundleName:(NSString *)bundleName{
return [self resourceBundleWithClassName:NSStringFromClass(self) bundleName:bundleName];
}
+ (instancetype)resourceBundleWithFramework:(NSString *)frameworkName
bundleName:(NSString *)bundleName{
NSURL *frameworksURL = [[NSBundle mainBundle] URLForResource:@"Frameworks" withExtension:nil];
if (!frameworkName) {
NSAssert(!frameworkName, @"获取资源路径bundle时,frameworkName为空");
}
NSURL* libFrameworkURL = [frameworksURL URLByAppendingPathComponent:frameworkName];
libFrameworkURL = [libFrameworkURL URLByAppendingPathExtension:@"framework"];
NSBundle *libFrameworkBundle = [NSBundle bundleWithURL:libFrameworkURL];
if (!libFrameworkBundle) {
NSAssert(!libFrameworkBundle, @"获取资源路径bundle时,获取framework的bundle为空");
}
NSURL* libResourceBundleUrl = [libFrameworkBundle URLForResource:bundleName withExtension:@"bundle"];
return [self bundleWithURL:libResourceBundleUrl];
}
+ (instancetype)getFrameworkBundleWithClassName:(NSString *)className{
if (!className) {
NSAssert(!className, @"获取资源路径bundle时,类名为空");
return nil;
}
NSBundle *frameworkBundle = [self bundleForClass:NSClassFromString(className)];
return frameworkBundle;
}
+ (instancetype)getFrameworkBundleWithFramework:(NSString *)frameworkName{
NSURL *frameworksURL = [[NSBundle mainBundle] URLForResource:@"Frameworks" withExtension:nil];
if (!frameworkName) {
NSAssert(!frameworkName, @"获取资源路径bundle时,frameworkName为空");
}
NSURL* libFrameworkURL = [frameworksURL URLByAppendingPathComponent:frameworkName];
libFrameworkURL = [libFrameworkURL URLByAppendingPathExtension:@"framework"];
NSBundle *libFrameworkBundle = [self bundleWithURL:libFrameworkURL];
return libFrameworkBundle;
}
+ (instancetype)getMainBundleWithResourceBundle:(NSString *)bundleName{
NSBundle *mainBundle = [NSBundle mainBundle];
NSURL *resourceUrl = [mainBundle URLForResource:bundleName withExtension:@"bundle"];
return [self bundleWithURL:resourceUrl];
}
@end
UIImage+Resource.h
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface UIImage (Resource)
+ (UIImage *)imageNamed:(NSString *)imageName className:(NSString *)className bundleName:(NSString *)bundleName;
+ (UIImage *)imageNamed:(NSString *)imageName className:(NSString *)className;
+ (UIImage *)imageNamed:(NSString *)imageName frameworkName:(NSString *)frameworkName;
+ (UIImage *)imageNamed:(NSString *)imageName framework:(NSString *)frameworkName bundleName:(NSString *)bundleName;
+ (UIImage *)imageNamed:(NSString *)imageName bundleName:(NSString *)bundleName;
@end
NS_ASSUME_NONNULL_END
UIImage+Resource.m
#import "UIImage+Resource.h"
#import "NSBundle+Resource.h"
@implementation UIImage (Resource)
+ (UIImage *)imageNamed:(NSString *)imageName className:(NSString *)className bundleName:(NSString *)bundleName{
NSBundle *bundle = [NSBundle resourceBundleWithClassName:className bundleName:bundleName];
UIImage *image = [UIImage imageNamed:imageName inBundle:bundle compatibleWithTraitCollection:nil];
return image;
}
+ (UIImage *)imageNamed:(NSString *)imageName framework:(NSString *)frameworkName bundleName:(NSString *)bundleName{
NSBundle *bundle = [NSBundle resourceBundleWithFramework:frameworkName bundleName:bundleName];
UIImage *image = [UIImage imageNamed:imageName inBundle:bundle compatibleWithTraitCollection:nil];
return image;
}
+ (UIImage *)imageNamed:(NSString *)imageName className:(NSString *)className{
NSBundle *bundle = [NSBundle getFrameworkBundleWithClassName:className];
UIImage *image = [UIImage imageNamed:imageName inBundle:bundle compatibleWithTraitCollection:nil];
return image;
}
+ (UIImage *)imageNamed:(NSString *)imageName frameworkName:(NSString *)frameworkName{
NSBundle *bundle = [NSBundle getFrameworkBundleWithFramework:frameworkName];
UIImage *image = [UIImage imageNamed:imageName inBundle:bundle compatibleWithTraitCollection:nil];
return image;
}
+ (UIImage *)imageNamed:(NSString *)imageName bundleName:(NSString *)bundleName{
if (!imageName || !bundleName) {
NSAssert(!imageName || !bundleName, @"image name 或者 bundle name 为空");
return nil;
}
NSBundle *bundle = [NSBundle getMainBundleWithResourceBundle:bundleName];
UIImage *image = [UIImage imageNamed:imageName inBundle:bundle compatibleWithTraitCollection:nil];
return image;
}
@end