在我們開發(fā)動態(tài)庫或者靜態(tài)庫的時(shí)候,如果其中用到一些資源圖片,此時(shí)這些圖片我們應(yīng)該如何放置呢?
一般有兩種方式,這里總結(jié)一下:
- 通過.bundle文件存儲(
.bundle可存儲xib等其他文件),此時(shí)如果在項(xiàng)目中使用到我們的framework時(shí),需要把這里的.bundle文件添加到項(xiàng)目中,這時(shí)我們可以用以下代碼去加載:
NSBundle *bundle = [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:@"Robot" ofType:@"bundle"]];
UIImage *iconImage= [UIImage imageWithContentsOfFile:[bundle pathForResource:imgName ofType:@"png"]];
當(dāng)然,如果我們不想把.bundle拖入項(xiàng)目中也是可以的,但是此時(shí)我們確認(rèn)打包的framework中含有.bundle,然后用以下方式去查找:
NSString *path = [[NSBundle mainBundle] pathForResource:@"RobotSDK" ofType:@"framework" inDirectory:@"Frameworks"];
NSBundle *bundle = [NSBundle bundleWithPath:path];
bundle = [NSBundle bundleWithPath:[bundle pathForResource:@"Robot" ofType:@"bundle"]];
UIImage *iconImage= [UIImage imageWithContentsOfFile:[bundle pathForResource:@"up" ofType:@"png"]];
- 通過.xcassets圖片管理工具來存儲(
只能用來存儲圖片),.xcassets在打包后會變成Assets.car,然而直接使用imageNamed:由于默認(rèn)位置無法找到framework的圖片資源,所以返回為nil,此時(shí)我們需要使用以下方式:
NSString *path = [[NSBundle mainBundle] pathForResource:@"RobotSDK" ofType:@"framework" inDirectory:@"Frameworks"];
NSBundle *bundle = [NSBundle bundleWithPath:path];
UIImage *iconImage = [UIImage imageNamed:imgName inBundle:bundle compatibleWithTraitCollection:nil];

image.png
另外補(bǔ)充:項(xiàng)目中如果引入多個動態(tài)庫,并且這多個動態(tài)庫中都同時(shí)引入了一個或者多個同樣的動態(tài)庫,此時(shí)我們只需要在項(xiàng)目中的動態(tài)庫后面添加"Embed & Sign",其他動態(tài)庫后為"Do Not Embed"(這樣設(shè)置后,sdk打包后不會把framework打入其中)如下:

image.png

image.png
參考文獻(xiàn):