现在我有一个嵌入主项目中的SDK项目,我想使用SDK项目中的 bundle 中的图像源,这是我的方法:
let bdpath = Bundle.main.path(forResource: "AltynBankResource", ofType: "bundle")
print("bundlePath:\(bdpath)")
let bd = Bundle(path: bdpath ?? "")
print("bundle:\(bd)")
let img = UIImage.init(named: "ic_bg1", in: bd, compatibleWith: nil)
print("img:\(img)")
但它不起作用。直到我将 bundle 源复制到主项目。
但我不明白为什么,我没有在主项目中使用该图像,而是在SDK文件中使用该图像。为什么我必须将 bundle 源复制到主项目才能使用它?
最佳答案
您面临的问题是您正在将主 bundle 与 Bundle.main
一起使用,这将占用您的应用程序的 bundle 。
我假设这段代码位于您的 SDK 中。我通常做的是在 Objective-C 中使用 [NSBundle bundleForClass:[self class]]
获取 SDK 的 bundle 。
在 swift3.x 中是:
bundle (for:type(of:self))
但是我无法为您提供 swift 的示例,因为我没有在 swift 中使用过这行代码。
希望这有帮助。
关于ios - 为什么我必须将 Bundle 资源复制到主项目才能在框架中使用图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53334691/