简体   繁体   中英

I can't find my AppIcon in the main bundle

I have this piece of code ...

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

This is returning nil, which I can't seem to figure out. I assume my file path is wrong. Any suggestions?

Try this. I hope this may be helpful to you.

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
  }
}

You can use it in your app like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM