gpt4 book ai didi

ios - 如何在 cocoapods resource_bundle 中加载资源

转载 作者:IT王子 更新时间:2023-10-29 07:58:40 26 4
gpt4 key购买 nike

我为如何在 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com