iOS中Cocoapods的Bundle

用Cocoapods做過第三方庫的同學應該都知道資源文件(圖片、多語言文件等)要放在bundle里面

podspec資源文件寫法

  • 需要打Bundle
    s.resources = "Classes/Resources/CTAssetsPickerController.bundle"

s.resources = "你bundle文件的路徑"

  • 不需要打Bundle
    spec.resource_bundles = { 'CTAssetsPickerController' => ['CTAssetsPickerController/Resources/CTAssetsPicker.xcassets/*/*.png', 'CTAssetsPickerController/Resources/*.lproj'] }

或者
spec.resource_bundles = { 'CTAssetsPickerController' => ['CTAssetsPickerController/Resources/**/*'] }

**/*這個表示所有文件,包括文件夾

spec.resource_bundles = { '你的庫名' => ['資源文件路徑/**/*'] }


使用

  • 多語言文件
 extension String {
    var calocale: String {
// 1.創(chuàng)建bundle
        //        let path = Bundle(for: CTAssetsPickerController.self).path(forResource: "CTAssetsPickerController", ofType: "bundle")!

// 這里for:類名可以隨便,只要是你庫里面的就行。
//  后面.bundle的文件名不能錯

// 2.創(chuàng)建bundle
        guard let path = Bundle(for: CTAssetCheckmark.self).path(forResource: "CTAssetsPickerController", ofType: "bundle") else {
            return nil
        }
        let CABundle = Bundle(path: path)

        return NSLocalizedString(self, tableName: "CTAssetsPicker", bundle: CABundle, value: "", comment: "")
    }
}
  • 圖片
private var bundleImage: UIImage? {
        guard let path = Bundle(for: CTAssetCheckmark.self).path(forResource: "CTAssetsPickerController", ofType: "bundle") else {
            return nil
        }
        let CABundle = Bundle(path: path)
        return UIImage(named: "你要讀取的圖片", in:  CABundle, compatibleWith: nil)
    }

參考博客

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

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

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