Load app icon from xcassets

前端 未结 3 1363
暖寄归人
暖寄归人 2021-02-02 11:11

I\'d like to display the app icon inside my app. The icon is in the default assets catalog (Images.xcassets).

How do you load it? I tried the following and

3条回答
  •  囚心锁ツ
    2021-02-02 11:39

    Following Ortwin answer, a Swift 4 approach:

    func getHighResolutionAppIconName() -> String? {
        guard let infoPlist = Bundle.main.infoDictionary else { return nil }
        guard let bundleIcons = infoPlist["CFBundleIcons"] as? NSDictionary else { return nil }
        guard let bundlePrimaryIcon = bundleIcons["CFBundlePrimaryIcon"] as? NSDictionary else { return nil }
        guard let bundleIconFiles = bundlePrimaryIcon["CFBundleIconFiles"] as? NSArray else { return nil }
        guard let appIcon = bundleIconFiles.lastObject as? String else { return nil }
        return appIcon
    }
    

    Then it can be used like:

    let imageName = getHighResolutionAppIconName()
    myImageView.image = UIImage(named: imageName)
    

提交回复
热议问题