我在主捆绑包中找不到我的AppIcon

时间:2019-12-16 00:11:03

标签: ios swift xcode

我有这段代码...

guard let filePath = Bundle.main.path(forResource: "AppIcon", ofType: "png") else {
        print("Image not found")
        return nil
    }

这返回nil,我似乎无法弄清楚。我认为我的文件路径错误。有什么建议么?

1 个答案:

答案 0 :(得分:1)

尝试一下。希望对您有帮助。

extension Bundle {
 public var icon: UIImage? {
    if let icons = infoDictionary?["CFBundleIcons"] as? [String: Any],
        let primaryIcon = icons["CFBundlePrimaryIcon"] as? [String: Any],
        let iconFiles = primaryIcon["CFBundleIconFiles"] as? [String],
        let lastIcon = iconFiles.last {
        return UIImage(named: lastIcon)
    }
    return nil
  }
}

您可以像这样在应用程序中使用它:

let imageView = UIImageView()
imageView.image = Bundle.main.icon