Pod庫(kù)圖片資源管理

CocoaPods中管理圖片資源

CocoaPods在.podspec文件中描述資源的字段有兩個(gè),resources和resouce_bundles。

resources

使用方式如下:

spec.resource = 'Resources/HockeySDK.bundle'
spec.resources = ['Images/*.png', 'Sounds/*']

CocoaPods不推薦使用resources,因?yàn)樗鼤?huì)產(chǎn)生圖片資源沖突。

使用"*.png"描述資源

此時(shí)打包之后Pod庫(kù)圖片會(huì)出現(xiàn)在安裝包里,沒有進(jìn)入主工程的Assets.car中,不同Pod庫(kù)中相同名字的圖片會(huì)在安裝包里沖突導(dǎo)致被覆蓋掉。

使用"*.bundle"描述資源

此時(shí)打包之后Pod庫(kù)的bundle文件會(huì)出現(xiàn)在安裝包里,不同Pod庫(kù)的同名bundle文件會(huì)融合成一個(gè)bundle文件,而bundle文件中相同名字的圖片就會(huì)沖突然后被覆蓋掉。

使用"*.xcassets"描述資源

此時(shí)Pod庫(kù)的.xcassets中的圖片會(huì)被打包到主工程的Assets.car中,不同Pod庫(kù)中相同名字的圖片會(huì)在Assets.car中出現(xiàn)沖突然后被覆蓋。

上面詳細(xì)描述了使用“resources”來(lái)描述圖片資源時(shí),打包之后圖片所在的位置,及造成圖片沖突的原因。

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)部通過一對(duì)key-value來(lái)描述資源,對(duì)每一個(gè)key生成一個(gè)bundle,value則是該bundle下的圖片資源;為了減少key沖突,CocoaPods建議key至少包含Pod的名字,同一個(gè)Pod庫(kù)下不同的bundle在Pod的名字之后加上不同的后綴。

CocoaPods推薦使用resource_bundles來(lái)管理圖片資源,但是有一個(gè)非常嚴(yán)重的問題需要注意:https://github.com/CocoaPods/CocoaPods/issues/8431,就是當(dāng)項(xiàng)目中有一個(gè)Pod庫(kù)采用了resources&xcasset的方式描述資源時(shí),項(xiàng)目所有Pod庫(kù)中的xcasset都會(huì)被拷貝到主工程的Assets.car中;此時(shí)如果有一個(gè)Pod庫(kù)使用了resource_bundles&xcasset描述資源時(shí),該P(yáng)od的xcasset中的圖片也會(huì)被拷貝到主工程的Assets.car中,這會(huì)與Pod庫(kù)的自己bundle中生成的Assets.car圖片造成重復(fù),產(chǎn)生這個(gè)問題的地方在于執(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的作者按時(shí)安排在1.9版本中解決這個(gè)問題。

CocoaPods中圖片資源的使用

NSBundle

再來(lái)熟悉一下文檔中對(duì)Bundle的描述,"Apple uses bundles to represent apps, frameworks, plug-ins, and many other specific types of content",也就是說(shuō)項(xiàng)目打包好的.app和frameworks都是一個(gè)Bundle。

查找并且打開一個(gè)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就是一個(gè)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)我們想要獲取一個(gè)Framework的Bundle時(shí),就可以通過這個(gè)方法,方法參數(shù)是這個(gè)framework中定義的一個(gè)類。需要注意的是如果當(dāng)前framework是靜態(tài)庫(kù)時(shí),該方法等同于[NSBundle mainBundle]。
// Get the bundle containing the specified private class.
NSBundle *myBundle = [NSBundle bundleForClass:[MyPrivateClass class]];

獲取當(dāng)前Pod的圖片資源

按照CocoaPods的建議,使用resource_bundles來(lái)描述資源文件,為了避免其他Pod庫(kù)使用resources&xcasset給我們的Pod庫(kù)造成麻煩,我們的Pod內(nèi)部使用*.png來(lái)管理圖片資源。

s.resource_bundles = {
  'XCCategory' => ['XCCategory/Assets/*.png']
}

我們目前Pod庫(kù)是靜態(tài)庫(kù),所以會(huì)在.app下生成一個(gè)"XCCategory.bundle"。

當(dāng)前Pod可能作為動(dòng)態(tài)庫(kù)或者靜態(tài)庫(kù),為了兼容這兩種情況,使用bundleForClass:來(lái)獲取Pod的bundle,當(dāng)Pod作為靜態(tài)庫(kù)時(shí),該方法返回的是mainBundle,當(dāng)Pod作為動(dòng)態(tài)庫(kù)時(shí),該方法返回的就是動(dòng)態(tài)庫(kù)本身。

下面代碼獲取到了XCCategory.bundle,然后從XCCategory.bundle中取出對(duì)應(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;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容