加载.bundle中资源

1、设置文件路径

NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"SourcesBundle" ofType:@"bundle"];

NSBundle *resourceBundle = [NSBundle bundleWithPath:bundlePath];

2、 加载 nib文件,获取 xib文件

UINib *nib = [UINib nibWithNibName:@"Demo" bundle:resourceBundle];

NSArray *viewObjs = [nib instantiateWithOwner:nil options:nil];

UIView *view = viewObjs.lastObject;

view.frame = CGRectMake(20,50,self.view.bounds.size.width -40,self.view.bounds.size.width -40);

[self.view addSubview:view];

3、VC获得bundle中的资源

 NSString * bundlePath = [[ NSBundle mainBundle] pathForResource: @ "MyBundle"ofType :@ "bundle"];

NSBundle *resourceBundle = [NSBundle bundleWithPath:bundlePath];

UIViewController *vc = [[UIViewController alloc] initWithNibName:@"vc_name"bundle:resourceBundle];

4、图片获得bundle中的资源

 UIImageView *imgView=[[UIImageView alloc] initWithFrame:CGRectMake(50, 50, 50,50)];

UIImage *image = [UIImage imageNamed:@"MyBundle.bundle/img_collect_success"];

[imgView setImage:image];

5、加载bundle中的xib生成的cell

加载nib的时候使用以下代码,最主要的是表明是从那个bundle中获取nib

 NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"WofuSDKBundle"ofType:@"bundle"];

 NSBundle *resourceBundle = [NSBundle bundleWithPath:bundlePath];

 UINib *nib=[UINib nibWithNibName:@"Wofucreditcell"bundle:resourceBundle];

  [tab registerNib:nib forCellReuseIdentifier:identifier];

猜你喜欢

转载自blog.csdn.net/weixin_40873814/article/details/82734412