CocoaPods中管理圖片資源
CocoaPods在.podspec文件中描述資源的字段有兩個,resources和resouce_bundles。
resources
使用方式如下:
spec.resource = 'Resources/HockeySDK.bundle'
spec.resources = ['Images/*.png', 'Sounds/*']
CocoaPods不推薦使用resources胞皱,因為它會產(chǎn)生圖片資源沖突毫炉。
使用"*.png"描述資源
此時打包之后Pod庫圖片會出現(xiàn)在安裝包里,沒有進入主工程的Assets.car中,不同Pod庫中相同名字的圖片會在安裝包里沖突導(dǎo)致被覆蓋掉您旁。
使用"*.bundle"描述資源
此時打包之后Pod庫的bundle文件會出現(xiàn)在安裝包里,不同Pod庫的同名bundle文件會融合成一個bundle文件轴捎,而bundle文件中相同名字的圖片就會沖突然后被覆蓋掉鹤盒。
使用"*.xcassets"描述資源
此時Pod庫的.xcassets中的圖片會被打包到主工程的Assets.car中,不同Pod庫中相同名字的圖片會在Assets.car中出現(xiàn)沖突然后被覆蓋侦副。
上面詳細描述了使用“resources”來描述圖片資源時侦锯,打包之后圖片所在的位置,及造成圖片沖突的原因秦驯。
resource_bundles
使用方式如下:
spec.ios.resource_bundle = { 'MapBox' => 'MapView/Map/Resources/*.png' }
spec.resource_bundles = {
'MapBox' => ['MapView/Map/Resources/*.png'],
'MapBoxOtherResources' => ['MapView/Map/OtherResources/*.png']
}
resource_bundles內(nèi)部通過一對key-value來描述資源尺碰,對每一個key生成一個bundle,value則是該bundle下的圖片資源;為了減少key沖突亲桥,CocoaPods建議key至少包含Pod的名字洛心,同一個Pod庫下不同的bundle在Pod的名字之后加上不同的后綴。
CocoaPods推薦使用resource_bundles來管理圖片資源题篷,但是有一個非常嚴重的問題需要注意:https://github.com/CocoaPods/CocoaPods/issues/8431词身,就是當項目中有一個Pod庫采用了resources&xcasset的方式描述資源時,項目所有Pod庫中的xcasset都會被拷貝到主工程的Assets.car中番枚;此時如果有一個Pod庫使用了resource_bundles&xcasset描述資源時法严,該Pod的xcasset中的圖片也會被拷貝到主工程的Assets.car中,這會與Pod庫的自己bundle中生成的Assets.car圖片造成重復(fù)葫笼,產(chǎn)生這個問題的地方在于執(zhí)行pod install之后生成的Pods-xxxxx-resource.sh腳本里的如下代碼中:
if [[ -n "${WRAPPER_EXTENSION}" ]] && [ "`xcrun --find actool`" ] && [ -n "${XCASSET_FILES:-}" ]
then
# Find all other xcassets (this unfortunately includes those of path pods and other targets).
OTHER_XCASSETS=$(find "$PWD" -iname "*.xcassets" -type d)
while read line; do
if [[ $line != "${PODS_ROOT}*" ]]; then
XCASSET_FILES+=("$line")
fi
done <<<"$OTHER_XCASSETS"
if [ -z ${ASSETCATALOG_COMPILER_APPICON_NAME+x} ]; then
printf "%s\0" "${XCASSET_FILES[@]}" | xargs -0 xcrun actool --output-format human-readable-text --notices --warnings --platform "${PLATFORM_NAME}" --minimum-deployment-target "${!DEPLOYMENT_TARGET_SETTING_NAME}" ${TARGET_DEVICE_ARGS} --compress-pngs --compile "${BUILT_PRODUCTS_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}"
else
printf "%s\0" "${XCASSET_FILES[@]}" | xargs -0 xcrun actool --output-format human-readable-text --notices --warnings --platform "${PLATFORM_NAME}" --minimum-deployment-target "${!DEPLOYMENT_TARGET_SETTING_NAME}" ${TARGET_DEVICE_ARGS} --compress-pngs --compile "${BUILT_PRODUCTS_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}" --app-icon "${ASSETCATALOG_COMPILER_APPICON_NAME}" --output-partial-info-plist "${TARGET_TEMP_DIR}/assetcatalog_generated_info_cocoapods.plist"
fi
fi
上述issue中提供了可以復(fù)現(xiàn)的demo深啤,而且CocoaPods的作者按時安排在1.9版本中解決這個問題。
CocoaPods中圖片資源的使用
NSBundle
再來熟悉一下文檔中對Bundle的描述路星,"Apple uses bundles to represent apps, frameworks, plug-ins, and many other specific types of content"墓塌,也就是說項目打包好的.app和frameworks都是一個Bundle。
查找并且打開一個NSBundle的創(chuàng)建方式大致分如下兩種:
- +[NSBundle mainBundle]
"The main bundle represents the bundle directory that contains the current executing code. So for an app, the main bundle object gives you access to the resources that shipped with your app." 可以理解為.app就是一個Bundle奥额。
// Get the app's main bundle
NSBundle *main = [NSBundle mainBundle];
- +[NSBundle bundleForClass:]
"if you link to a framework, you can use the bundleForClass: method to locate the framework bundle based on a class defined in that framework." 當我們想要獲取一個Framework的Bundle時苫幢,就可以通過這個方法,方法參數(shù)是這個framework中定義的一個類垫挨。需要注意的是如果當前framework是靜態(tài)庫時韩肝,該方法等同于[NSBundle mainBundle]。
// Get the bundle containing the specified private class.
NSBundle *myBundle = [NSBundle bundleForClass:[MyPrivateClass class]];
獲取當前Pod的圖片資源
按照CocoaPods的建議九榔,使用resource_bundles來描述資源文件哀峻,為了避免其他Pod庫使用resources&xcasset給我們的Pod庫造成麻煩,我們的Pod內(nèi)部使用*.png來管理圖片資源哲泊。
s.resource_bundles = {
'XCCategory' => ['XCCategory/Assets/*.png']
}
我們目前Pod庫是靜態(tài)庫剩蟀,所以會在.app下生成一個"XCCategory.bundle"。
當前Pod可能作為動態(tài)庫或者靜態(tài)庫切威,為了兼容這兩種情況育特,使用bundleForClass:來獲取Pod的bundle,當Pod作為靜態(tài)庫時先朦,該方法返回的是mainBundle缰冤,當Pod作為動態(tài)庫時,該方法返回的就是動態(tài)庫本身喳魏。
下面代碼獲取到了XCCategory.bundle棉浸,然后從XCCategory.bundle中取出對應(yīng)的圖片。
+ (nullable UIImage *)xccategory_imageName:(NSString *)name {
static NSBundle *resourceBundle = nil;
if (resourceBundle == nil) {
resourceBundle = [NSBundle bundleWithPath:[[NSBundle bundleForClass:[XCCategoryViewController class]] pathForResource:@"XCCategory" ofType:@"bundle"]];
}
UIImage *image = [UIImage imageNamed:name inBundle:resourceBundle compatibleWithTraitCollection:nil];
return image;
}