在组件化中,你的.assets中的图片,文件夹中的图片、gif图片,xib文件,json文件等都需要进行处理,否则找不到文件,加载不了,都会为nil。
我今天就使用[[NSBundle mainBundle] pathForResource:@"sender_avatar" ofType:@"svg"];加载pod本地库里面的图片资源,一直获取不到为nil。
1、在podspec设置生成bundle文件
在你的组件化文件pod -> .podspec文件中设置
BranOpenSourceModule/Assets.xcassets: 设置加载Assets.xcassets中的图片
BranOpenSourceModule/**/*.xib:设置加载xib文件
BranOpenSourceModule/Images/: 设置加载Module -> Images文件夹中的图片、gif图片、json文件等
s.resource_bundles = {
'Bran_bundles' => ['BranOpenSourceModule/Assets.xcassets', 'BranOpenSourceModule/**/*.xib', 'BranOpenSourceModule/Images/*']
}
2、在组件中加载文件
2.1、Objective-C
2.1.1 首先生成 NSBundle文件
PodBundle.h 封装
+ (NSBundle *)pod_ResourceBundle;
PodBundle.m 注意,此处的bundle文件名要和 .podspec 文件中s.resource_bundles设置的一致
static NSBundle *resourceBundle = nil;
static dispatch_once_t onceToken;
+ (NSBundle *)hj_ResourceBundle {
dispatch_once(&onceToken, ^{
NSString *resourceBundlePath = [[NSBundle bundleForClass:[self class]] pathForResource:@"Bran_bundles" ofType:@"bundle"];
resourceBundle = [NSBundle bundleWithPath:resourceBundlePath];
});
return resourceBundle;
}
2.1.2 加载图片
封装加载方法
@interface UIImage (Category)
+ (instancetype)pod_image:(NSString *)name;
@end
@implementation UIImage (Category)
+ (instancetype)pod_image:(NSString *)name {
return [UIImage imageNamed:name inBundle:[PodBundle pod_ResourceBundle] compatibleWithTraitCollection:nil];
}
@end
图片加载(.png)
UIImageView * imageView = [[UIImageView alloc] init];
imageView.image = [UIImage pod_image:@"sender_avatar"];
图片加载(.svg)--> 需要使用第三方库 pod 'SVGKit'
#pragma mark UI 懒加载
-(UIImageView *)avatarImageView {
if (!_avatarImageView) {
_avatarImageView = [[UIImageView alloc] init];
_avatarImageView.contentMode = UIViewContentModeScaleAspectFit;
SVGKImage *image = [SVGKImage imageNamed:@"sender_avatar.svg" inBundle:[PodBundle pod_ResourceBundle]];
_avatarImageView.image = image.UIImage;
}
return _avatarImageView;
}
2.1.3 gif 图片加载 --> 使用的是 FLAnimatedImage 加载本地gif
@interface FLAnimatedImage (Category)
+ (instancetype)pod_gif:(NSString *)name;
@end
@implementation FLAnimatedImage (Category)
+ (instancetype)pod_gif:(NSString *)name {
NSBundle * bundle = [PodBundle pod_ResourceBundle];
NSURL * url = [bundle URLForResource:name withExtension:@"gif"];
NSData * imageData = [NSData dataWithContentsOfURL:url];
return [FLAnimatedImage animatedImageWithGIFData:imageData];
}
@end
加载方法
FLAnimatedImageView * imageView = [[FLAnimatedImageView alloc] init];
imageView.image = [UIImage hj_imageDevice:@"sender_avatar"];
2.1.4 解析json
NSURL * url = [[PodBundle pod_ResourceBundle] URLForResource:name withExtension:@"json"];
NSData * imageData = [NSData dataWithContentsOfURL:url];
NSDictionary * json = [NSJSONSerialization JSONObjectWithData:imageData options:NSJSONReadingMutableContainers error:nil];
2.1.5 加载 xib
此处是原来加载的xib代码,无法加载
TCellOne
TCellOne *cell = [[TCellOne alloc] initWithNibName:nil bundle:nil];
更改后的
TCellOne *cell = [[TCellOne alloc] initWithNibName:@"TCellOne" bundle:[PodBundle pod_ResourceBundle]];
2.2 Swift
2.2.1 swift 加载图片
注意,此处的bundle文件名要和 .podspec 文件中设置的一致, 上面有说明,此处就不再次说明
extension UIImage {
class func yl_image(named name: String) -> UIImage{
return YLStudyAssets.bundledImage(named: name)
}
}
open class YLStudyAssets: NSObject {
internal class func bundledImage(named name: String) -> UIImage {
let primaryBundle = Bundle(for: YLStudyAssets.self)
if let image = UIImage(named: name, in: primaryBundle, compatibleWith: nil) {
return image
} else if
let subBundleUrl = primaryBundle.url(forResource: "Polyv_bundles", withExtension: "bundle"),
let subBundle = Bundle(url: subBundleUrl),
let image = UIImage(named: name, in: subBundle, compatibleWith: nil){
return image
}
return UIImage()
}
}
图片加载
let imageView = UIImageView()
imageView.image = UIImage.yl_image(named: "")
2.2.2 加载gif图片
此处使用FLAnimatedImage加载,注意,此处的bundle文件名要和 .podspec 文件中设置的一致
import FLAnimatedImage
extension FLAnimatedImage {
class func yl_gif(named name: String) -> FLAnimatedImage {
return YLStudyAssets.bundleGif(named: name)
}
}
open class YLStudyAssets: NSObject {
internal class func bundleGif(named name: String) -> FLAnimatedImage {
let primaryBundle = Bundle(for: YLStudyAssets.self)
if let subBundleUrl = primaryBundle.url(forResource: "YLStudy_bundles", withExtension: "bundle"),
let subBundle = Bundle(url: subBundleUrl),
let url = subBundle.url(forResource: name, withExtension: "gif"),
let imageData = try? Data(contentsOf: url) {
return FLAnimatedImage(animatedGIFData: imageData)
}
return FLAnimatedImage()
}
}
gif图片加载
private lazy var imageIcon: FLAnimatedImageView = {
let imageView = FLAnimatedImageView()
imageView.animatedImage = FLAnimatedImage.yl_gif(named: "voiceLeft")
return imageView
}()
2.2.3 加载json
我此处是使用lottie-ios 加载动图
let primaryBundle = Bundle(for: YLHomeAssets.self)
let subBundleUrl = primaryBundle.url(forResource: "YLHome_bundles", withExtension: "bundle")
let subBundle = Bundle(url: subBundleUrl!) ?? Bundle.main
let animation = Animation.named(fileName, bundle: subBundle)