1、如何创建一个bundle:
iOS 如何把图片资源打包成bundle文件及遇到的坑
2、把图片资源,xib打包进bundle以后,如何使用?
2.1、加载本地的bundle
+ (NSBundle *)bundle {
static NSBundle *bundle = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
bundle = [NSBundle bundleWithPath:[[NSBundle bundleForClass:[self class]] pathForResource:@"ZLYKit" ofType:@"bundle"]];
});
return bundle;
}
2.2、获取bundle里面的图片资源
+ (UIImage *)imageNamed:(NSString *)name {
UIImage *image = [UIImage imageNamed:name inBundle:self.bundle compatibleWithTraitCollection:nil];
return image;
}
+ (UIImage *)imageNamed:(NSString *)name type:(NSString *)type {
if (name.length == 0) return nil;
int scale = (int)UIScreen.mainScreen.scale;
if (scale < 2) scale = 2;
else if (scale > 3) scale = 3;
NSString *n = [NSString stringWithFormat:@"%@@%dx", name, scale];
UIImage *image = [UIImage imageWithContentsOfFile:[self.bundle pathForResource:n ofType:type]];
if (!image) image = [UIImage imageWithContentsOfFile:[self.bundle pathForResource:name ofType:type]];
return image;
}
[self.leftImageView sd_setImageWithURL:[NSURL URLWithString:fileModel.courseImageUrl] placeholderImage:[ZLYUtilities imageNamed:@"place_holder_home_lesson"]];
2.3、获取bundle里面的xib资源
[_tableView registerNib:[UINib nibWithNibName:@"LYChatRoomImageTableViewCell" bundle:[ZLYUtilities bundle]] forCellReuseIdentifier:imageID];