為了組件化,我們的工程會(huì)將一些公用的組件進(jìn)行pod制作,如果你封裝的組件是有關(guān)于UI的,那么pod中可能會(huì)用到圖片資源,如果只是簡(jiǎn)單的使用[UIImage imageWithName:]方法可能會(huì)獲取不到image,這個(gè)可能是由于對(duì)應(yīng)的資源調(diào)用的類(lèi)可能沒(méi)有找到自己關(guān)聯(lián)的bundle中的資源。解決方法如下:
1、先獲取調(diào)用資源的類(lèi)的關(guān)聯(lián)bundle,NSBundle*myBundle = [NSBundlebundleForClass:[ClassA class]];
2、獲取該bundle所在的path,然后拼接圖片資源名稱(chēng):
NSString *path = [[ myBundle resourcePath]stringByAppendingPathComponent:name]];
3、使用image = [UIImageimageWithContentsOfFile:path];獲取圖片
參考鏈接:
ios - Pod (PaymentKit) can't load images using imageNamed - Stack Overflow
ios - Impossible to load an image in xcassets on bundle - Stack Overflow