2018年11月7日
模块化后,经常可能出现图片不显示,或xib资源文件找不到导致奔溃
原因:原来都是在主工程获取,所以都能找到,目前将资源文件都移到对应的库里面,所以按原来的用法到主工程就考不到了,所以如果怕麻烦就全部纯代码开发即可。
一.xib文件
1模块化前
使用:
-(UITableView *)tableView{
if (!_tableView) {
_tableView = [[UITableView alloc] init];
_tableView.frame = CGRectMake(0, 0, HHBWIDTH, HHBHEIGHT - nav_height);
[_tableView registerNib:[UINib nibWithNibName:@"HuCourseExchangeCell" bundle:nil] forCellReuseIdentifier:kCellIdentifier];
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
_tableView.tableFooterView = [UIView new];
[self.view addSubview:_tableView];
WS(weakSelf);
[HuTableViewRefresh tableViewRefresh:_tableView success:^(refreshType refresh) {
refresh == TableViewHeaderRefresh ? weakSelf.pageNum = kPageNum: weakSelf.pageNum++;
[weakSelf loadingData];
}];
}
return _tableView;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
CourseAnswerModel *model = [(CourseAnswerModel *)[self.answerArray objectAtIndex:indexPath.section] childList][indexPath.row];
HuCourseExchangeCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier forIndexPath:indexPath];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.model = model;
return cell;
}
2.模块化后
xib文件路径修改了,放到Assets里面(pod之后就会在Resources显示 )
使用:
-(UITableView *)tableView{
if (!_tableView) {
_tableView = [[UITableView alloc] init];
_tableView.frame = CGRectMake(0, 0, HHBWIDTH, HHBHEIGHT - nav_height);
NSBundle *bundle = [NSBundle wg_subBundleWithBundleName:HuDiscoveryKit targetClass:[self class]];
[_tableView registerNib:[UINib nibWithNibName:@"HuCourseExchangeCell" bundle:bundle] forCellReuseIdentifier:kCellIdentifier];
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
_tableView.tableFooterView = [UIView new];
[self.view addSubview:_tableView];
WS(weakSelf);
[HuTableViewRefresh tableViewRefresh:_tableView success:^(refreshType refresh) {
refresh == TableViewHeaderRefresh ? weakSelf.pageNum = kPageNum: weakSelf.pageNum++;
[weakSelf loadingData];
}];
}
return _tableView;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
CourseAnswerModel *model = [(CourseAnswerModel *)[self.answerArray objectAtIndex:indexPath.section] childList][indexPath.row];
HuCourseExchangeCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier forIndexPath:indexPath];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.model = model;
return cell;
}
//扩展方法
#import <Foundation/Foundation.h>
@interface NSBundle (wgSubBundle)
+ (instancetype)wg_subBundleWithBundleName:(NSString *)bundleName targetClass:(Class)targetClass;
@end
#import "NSBundle+wgSubBundle.h"
@implementation NSBundle (wgSubBundle)
+ (instancetype)wg_subBundleWithBundleName:(NSString *)bundleName targetClass:(Class)targetClass{
//并没有拿到子bundle
NSBundle *bundle = [NSBundle bundleForClass:targetClass];
//在这个路径下找到子bundle的路径
NSString *path = [bundle pathForResource:bundleName ofType:@"bundle"];
//根据路径拿到子bundle
return path?[NSBundle bundleWithPath:path]:[NSBundle mainBundle];
/*
这种方式也可以
NSBundle *bundle = [NSBundle bundleForClass:targetClass];
NSURL *url = [bundle URLForResource:bundleName withExtension:@"bundle"];
return path?[NSBundle bundleWithURL:url]:[NSBundle mainBundle];
*/
}
@end
2.1注意点,因为DiscoverAnswerIcon.png 这个图片是在xib使用了,如果我们还将改图片放在主工程,运行的时候,图片还是不会显示的,需要将它和xib文件放一起
3.常用例子
xib
1.有bundle参数的修改
[_tableView registerNib:[UINib nibWithNibName:@"QuestionAnswerTableViewCell" bundle:bundle] forCellReuseIdentifier:kCellIdentifier];
cell = [[bundle loadNibNamed:@"TrainingCourseDetailTableViewCell" owner:self options:nil] firstObject];
或者
HuShareCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier];
if (!cell) {
NSBundle *bundle = [NSBundle wg_subBundleWithBundleName:@"HuDiscoveryKit" targetClass:[self class]];
cell = [[bundle loadNibNamed:@"HuShareCell" owner:nil options:nil]firstObject];
}
2. 没有budle参数的修改
- (instancetype)init{
NSBundle *bundle = [NSBundle wg_subBundleWithBundleName:@"HuHealtheducationKit" targetClass:[self class]];
if (self = [super initWithNibName:@"HuIllnessDefaultViewController" bundle:bundle]) {
}
return self;
}
二.图片问题处理,(如果开发资源充沛,建议还是将各个库的图片放到各个库去)
1.由于项目较多,我们就没有将所有图片放到各自库去,默认都放在主工程里面(除了上面那种xib不显示图片问题)
还是用原来的用法
_errImage.image = [UIImage imageNamed:@"icon_error-circle"];
2.如果你要放到对应库里面,用法就要修改了
//NSBundle *bundle = [NSBundle wg_subBundleWithBundleName:@"HuHealtheducationKit" targetClass:[self class]];
NSString *bundleName = @“HuHealtheducationKit";
_videoDefaultView.image = [UIImage hu_imgWithName:@"icon_error-circle" bundle:bundleName targetClass:[self class]];
//扩展
#import <UIKit/UIKit.h>
@interface UIImage (HuBundle)
+ (instancetype)hu_imgWithName:(NSString *)name bundle:(NSString *)bundleName targetClass:(Class)targetClass;
@end
#import "UIImage+HuBundle.h"
@implementation UIImage (HuBundle)
+ (instancetype)hu_imgWithName:(NSString *)name bundle:(NSString *)bundleName targetClass:(Class)targetClass{
NSInteger scale = [[UIScreen mainScreen] scale];
NSBundle *curB = [NSBundle bundleForClass:targetClass];
NSString *imgName = [NSString stringWithFormat:@"%@@%zdx.png", name,scale];
NSString *dir = [NSString stringWithFormat:@"%@.bundle",bundleName];
NSString *path = [curB pathForResource:imgName ofType:nil inDirectory:dir];
return path?[UIImage imageWithContentsOfFile:path]:nil;
}
@end
3.补充
2018年10月19日
1.组件里面做图片库的两种方式
#define ZFPlayerSrcName(file) [@"ZFPlayer.bundle" stringByAppendingPathComponent:file]
#define ZFPlayerFrameworkSrcName(file) [@"Frameworks/HuZFPlayer.framework/ZFPlayer.bundle" stringByAppendingPathComponent:file]
#define ZFPlayerImage(file) [UIImage imageNamed:ZFPlayerSrcName(file)] ? :[UIImage imageNamed:ZFPlayerFrameworkSrcName(file)]
第二种方法
如果您发现本文对你有所帮助,如果您认为其他人也可能受益,请把它分享出去。