作者热门文章
- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我为如何在 cocoapods resource_bundle 中加载资源而苦苦挣扎。
以下是我放入.podspecs
文件的内容。
s.source_files = 'XDCoreLib/Pod/Classes/**/*'
s.resource_bundles = {
'XDCoreLib' => ['XDCoreLib/Pod/Resources/**/*.{png,storyboard}']
}
这就是我在主项目中尝试做的事情。
let bundle = NSBundle(forClass: XDWebViewController.self)
let image = UIImage(named: "ic_arrow_back", inBundle: bundle, compatibleWithTraitCollection: nil)
print(image)
我确实在 XDCoreLib.bundle
中看到了图片,但它返回了 nil。
最佳答案
我曾为类似的问题苦苦挣扎了一段时间。 pod 的资源包实际上是与您的代码所在位置不同的包。尝试以下操作:
swift 5
let frameworkBundle = Bundle(for: XDWebViewController.self)
let bundleURL = frameworkBundle.resourceURL?.appendingPathComponent("XDCoreLib.bundle")
let resourceBundle = Bundle(url: bundleURL!)
let image = UIImage(named: "ic_arrow_back", in: resourceBundle, compatibleWith: nil)
print(image)
-- 原始答案 --
let frameworkBundle = NSBundle(forClass: XDWebViewController.self)
let bundleURL = frameworkBundle.resourceURL?.URLByAppendingPathComponent("XDCoreLib.bundle")
let resourceBundle = NSBundle(URL: bundleURL!)
let image = UIImage(named: "ic_arrow_back", inBundle: resourceBundle, compatibleWithTraitCollection: nil)
print(image)
关于ios - 如何在 cocoapods resource_bundle 中加载资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35692265/
我是一名优秀的程序员,十分优秀!