Pod本地私有庫使用圖片和XIB資源文件

前言

制作本地本地POD庫就不做過多的介紹,因為本身就一個命令的事pod lib create xxxx

pod庫

pod管理項目的時候,如果用到了圖片或者XIB文件一般有兩種寫法:resources或者resource_bundles

Example

 spec.resources = "CXSalesmanModule/**/*.{xib,png}"

spec.resource_bundles = {
    'CXSalesmanModule' => ['CXSalesmanModule/**/*.{xib,png,xcassets}']
  }

先來說說區別:

  • 利用 resources 屬性可以指定 pod 要使用的資源文件。這些資源文件在build 時會被直接拷貝到 client targetmainBundle 里。這樣就實現了把圖片、音頻、NIB等資源打包進最終應用程序的目的。但是這會導致POD庫的資源文件和主工程里的資源文件命名沖突。

Example

主工程有一個a.png的圖片,而pod庫里面也有一個a.png的圖片,此時就產生命名沖突了。

  • resource_bundles就是為了解決命名沖突的問題,CocoaPods0.23.0 加入的新屬性。
  • resource_bundles會自動生成bundle把資源文件打包進去,resources則不會。所以我們在使用resources的時候一般都會把資源文件提前打包到bundle最后在添加。

建議使用resource_bundles方式來管理資源文件

use_frameworks重點

OC項目pod init的時候一般是不使用use_frameworks!,但是當我們用cocoapods導入swift框架到swift項目和OC項目都必須要use_frameworks!

對于Podfile有或者沒有使use_frameworks;resources或者resource_bundles這兩種寫法的最后編譯之后生成的包是不一樣的。

  • 如果使用了use_frameworks編譯之后查看包,我們會發現POD庫是放在mainBundle下的Frameworks目錄下。
  • 沒有使用use_frameworks,則不會生成Frameworks

pod install編譯之后我們來看下資源文件打包到哪里了

show in finder
products

使用spec.resources寫法

 spec.resources = ["CXSalesmanModule/**/*.{xib,png}"]

沒有使用user_frameworks
resources沒有使用user_frameworks

如果使用圖片或者XIB,因為資源文件是直接打包到和主工程的bundle也就是mainBundle,所以我們依舊可以和之前的寫法一樣:

使用

self.imagView.image = [UIImage imageNamed:@"icon_mine_grade"];
// xib 這里暫未做測試
使用了user_frameworks
resources使用user_frameworks

此時我們發現pod庫里面的資源文件被打包進了主工程(即:mainBundle)下的Frameworks->CXSalesmanModule.framework目錄下:
所以我們使用資源文件的時候,就不能直接加載mainBundle;我們需要找到資源文件所在的bundle

獲取bundle的兩種方式

通過class類型查找對應的bundle目錄,這種在category中不能使用,雖然可以通過傳入class的方式查找,但是容易出錯。不建議使用

NSBundle *cbundle = [NSBundle bundleForClass:[self class]]; 
NSString *path = [bundle pathForResource:bundleName ofType:@"bundle"]; 
NSBundle *bundle = [NSBundle bundleWithPath:path];

使用

NSURL *associateBundleURL = [[NSBundle mainBundle] URLForResource:@"Frameworks" withExtension:nil];
associateBundleURL = [associateBundleURL URLByAppendingPathComponent:@"CXSalesmanModule"];
associateBundleURL = [associateBundleURL URLByAppendingPathExtension:@"framework"];
NSBundle *bundle = [NSBundle bundleWithURL:associateBundleURL];
self.imagView.image = [UIImage imageNamed:@"icon_mine_grade"
  inBundle: associateBunle
compatibleWithTraitCollection:nil];

XIB同理也是通過Bundle去加載。

使用spec.resource_bundles寫法

spec.resource_bundles = {
  'CXSalesmanModule' => ['CXSalesmanModule/**/*.{xib,png,xcassets}']
}
沒有使用user_frameworks
resource_bundles沒有使用user_frameworks

此時我們發現pod庫里面的資源文件是被打包進了主工程(即:mainBundle)里面的CXSalesmanModule.bundle內,所以我們使用的話,只需要拿到這個bundle即可。這里也驗證了上面所說的resource_bundles會默認生成bundle

使用

NSURL *url = [[NSBundle mainBundle] URLForResource:@"CXSalesmanModule" withExtension:@"bundle"];
NSBundle *bundle = [NSBundle bundleWithURL:url];
self.imagView.image = [UIImage imageNamed:@"icon_mine_grade"
  inBundle: bundle
compatibleWithTraitCollection:nil];
使用了user_frameworks
resource_bundles使用了user_frameworks

此時我們發現pod庫里面的資源文件是被打包進了主工程(即:mainBundle)下的Frameworks->CXSalesmanModule.framework->CXSalesmanModule.bundle目錄下:
所以我們使用資源文件的時候,就不能直接加載mainBundle;我們需要找到資源文件所在的CXSalesmanModule.bundle,這里也驗證了上面所說的resource_bundles會默認生成bundle

使用

NSURL *associateBundleURL = [[NSBundle mainBundle] URLForResource:@"Frameworks" withExtension:nil];
associateBundleURL = [associateBundleURL URLByAppendingPathComponent:@"CXSalesmanModule"];
associateBundleURL = [associateBundleURL URLByAppendingPathExtension:@"framework"];
NSBundle *associateBunle = [NSBundle bundleWithURL:associateBundleURL];
associateBundleURL = [associateBunle URLForResource:@"CXSalesmanModule" withExtension:@"bundle"];
NSBundle *bundle = [NSBundle bundleWithURL:associateBundleURL];
self.imagView.image = [UIImage imageNamed:@"icon_mine_grade"
  inBundle: bundle
compatibleWithTraitCollection:nil];

XIB同理也是通過Bundle去加載。

總的來說,使用pod庫里面的資源文件,我們只需要找資源文件所在的路徑即可,如果是mainBundle則使用方式不變,如果是其他的bundle,我們只要獲取到bundle就可以通過bundle去使用。

圖片存放方式

  • 直接將png格式的圖片拖到Assets目錄下。
  • 采用xcassets,將圖片都放到Images.xcassets里面,新建項目的時候默認工程會有一個Assets.xcassets。

這里的圖片是采用xcassets來打包的,按住command + n選擇Asset Catalog即可。

資源文件
生成Asset

一般我們都是直接把圖片放到相應的目錄下,這里我要說的是resource_bundles打包圖片使用xcassets的注意點

注意(低于iOS10的系統)

對于pod資源打包方式采用resource_bundles并且podfile里使用了user_framework,如果采用.xcassets方式打包圖片,iOS9 Release環境下圖片會加載不出來。如果未使用user_framework則可以正常展示(iOS8暫沒有測試,以及采用resources來打包這里本人暫未做測試有興趣的小伙伴可以去測試一波)

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。