ios - 从 bundle 的 Assets 文件夹中加载图像

标签 ios swift swift3

我有一个包,里面有 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
}

这是我的项目结构:

enter image description here

你能看出项目的代码/结构有什么问题吗?

更新! ...图像针对所有分辨率设置为通用: enter image description here

最佳答案

要检查两件事。

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/

相关文章:

ios - 移除 ViewController 消失时的键盘通知

java - 将 Java 代码导入 Swift iOS 应用程序?

swift - 在完成 if 语句之前运行以下脚本

swift - 如何在 Swift 3 中访问 CFDictionary?

ios - Swift 3 时区问题

ios - 使用 SwiftUI 获取 AVPlayerItem 轨道标题

ios - 具有自动布局的 UIScrollView 中的 UITableViews

ios - 修复仅纵向模式或横向的应用程序

ios - 如何在 Realm 迁移中执行查询?

ios - Alamofire 4.0 上传时的模糊引用