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)在安裝包里,沒有進(jì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)沖突然后被覆蓋。
上面詳細(xì)描述了使用“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來管理圖片資源,但是有一個非常嚴(yán)重的問題需要注意:https://github.com/CocoaPods/CocoaPods/issues/8431,就是當(dāng)項目中有一個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." 當(dāng)我們想要獲取一個Framework的Bundle時,就可以通過這個方法,方法參數(shù)是這個framework中定義的一個類。需要注意的是如果當(dāng)前framework是靜態(tài)庫時,該方法等同于[NSBundle mainBundle]。
// Get the bundle containing the specified private class.
NSBundle *myBundle = [NSBundle bundleForClass:[MyPrivateClass class]];
獲取當(dāng)前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"。
當(dāng)前Pod可能作為動態(tài)庫或者靜態(tài)庫,為了兼容這兩種情況,使用bundleForClass:來獲取Pod的bundle,當(dāng)Pod作為靜態(tài)庫時,該方法返回的是mainBundle,當(dāng)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;
}