Here I have wrote one snippet for how to access an image through Assets programmatically:
It is not easy to access the AppIcon like we access other images from Assets.
Reason, the AppIcon from the asset catalog is not placed in the catalog after compiling rather it gets copied into the Apps bundle.
After copied into the Apps bundle, it gets shuffled.
An extension that makes you easy to access the App icon through Bundle.
1 2 3 4 5 6 7 8 9 10 11 |
extension Bundle { public var appIcon: UIImage? { if let appIcons = infoDictionary?["CFBundleIcons"] as? [String: Any], let primaryAppIcon = appIcons["CFBundlePrimaryIcon"] as? [String: Any], let appIconFiles = primaryAppIcon["CFBundleIconFiles"] as? [String], let lastAppIcon = appIconFiles.last { return UIImage(named:lastAppIcon) } return nil } } |
iconFiles.last : This line helps in getting the last image of the app icon.
Calling an extension in ViewController to display an image from AppIcon:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
class ViewController: UIViewController { @IBOutlet weak var imgView: UIImageView! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. //use extension let appIcon = Bundle.main.appIcon //returns image from AppIcon imgView.image = appIcon } } |
I hope from this, it will make you more comfortable getting the App Icon image from assets.
Thanks for tuning in once again!