我有一个包,里面有 Assets 文件夹。我已经阅读了堆栈上关于使用 UIImage(named: "drop_arrow", inBundle: bundle, compatibleWithTraitCollection: nil)
的所有答案(好吧,它相当于 swift 3)
path = Bundle.main.path(forResource: "LiveUI", ofType: "bundle")
if path != nil { // path with bundle is found
let bundle: Bundle = Bundle.init(path: path!)! // bundle is there
let image: UIImage? = UIImage(named: "HomePlayButton", in: bundle, compatibleWith: nil)
// HomePlayButton exists in the bundle/asset folder
// but image is nil
}
这是我的项目结构:
你能看出项目的代码/结构有什么问题吗?
最佳答案
要检查两件事。
1) 确保您的 Assets 包有一个 Info.plist,其中设置了一个包标识符。从您的屏幕截图来看,情况并非如此。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleIdentifier</key>
<string>MyBundle</string>
</dict>
</plist>
2) 确保你的 bundle 中有一个已编译的 Assets 目录。应命名为 Assets.car。我认为 Xcode 不会只编译资源包内的 Assets.xcassets 文件夹(资源包对 Xcode 来说实际上是不透明的;同样它不会编译您放入其中的任何 .m 源文件)。在将 Assets 包复制到应用程序包之前,您可能需要自定义构建步骤将已编译的 Assets.car 文件复制到您的 Assets 包中。
您可以通过找到您的应用程序包并右键单击它然后“显示包内容”,然后再次单击包含的包来检查这些。
手动资源编译命令:
xcrun actool Images.xcassets --compile . --platform iphoneos --minimum-deployment-target 9.0
关于ios - 从 bundle 的 Assets 文件夹中加载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39579389/