載入.bundle中資源
阿新 • • 發佈:2018-12-10
1、設定檔案路徑
NSString *bundlePath = [[NSBundlemainBundle]pathForResource:@"SourcesBundle"ofType:@"bundle"];
NSBundle *resourceBundle = [NSBundlebundleWithPath:bundlePath];
2、 // 載入 nib檔案
UINib *nib = [UINibnibWithNibName:@"Demo"bundle:resourceBundle]; NSArray *viewObjs = [nibinstantiateWithOwner:niloptions:nil];
3、 獲取 xib檔案
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];
4、VC獲得bundle中的資源
NSString * bundlePath = [[ NSBundle mainBundle] pathForResource: @ "MyBundle"ofType :@ "bundle"]; NSBundle *resourceBundle = [NSBundle bundleWithPath:bundlePath]; UIViewController *vc = [[UIViewController alloc] initWithNibName:@"vc_name"bundle:resourceBundle];
5、圖片獲得bundle中的資源
UIImageView *imgView=[[UIImageView alloc] initWithFrame:CGRectMake(50, 50, 50,50)];
UIImage *image = [UIImage imageNamed:@"MyBundle.bundle/img_collect_success"];
[imgView setImage:image];
6、載入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];