How to get the current application icon in ios

后端 未结 9 2084
心在旅途
心在旅途 2020-12-09 03:05

Is there a way to get the current application icon in a cocoa-touch app? Thank you.

相关标签:
9条回答
  • 2020-12-09 03:32

    This is how the icons is set inside the info.plist,

    //try doing NSLog(@"%@",[[NSBundle mainBundle] infoDictionary]);
    
     CFBundleIcons =     {
            CFBundlePrimaryIcon =         {
                CFBundleIconFiles =             (
                    "icon.png",//App icon added by you
                    "icon@2x.png"//App icon in retina added by you
                );
                UIPrerenderedIcon = 1;
            };
        };
    

    If you want to get the app icon use below code:

    UIImage *appIcon = [UIImage imageNamed: [[[[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleIcons"] objectForKey:@"CFBundlePrimaryIcon"] objectForKey:@"CFBundleIconFiles"] objectAtIndex:0]];
    

    P.S: UIImage automatically picks the suitable icon for retina display.

    I hope it helps!!

    0 讨论(0)
  • 2020-12-09 03:33

    Works for Swift 4.1 and extending it for Bundle.

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

    To use in an app, call Bundle.main.icon.

    0 讨论(0)
  • 2020-12-09 03:33

    I based my answer completely on moosgummi's, but returning a UIImage instead.

    UIImage *appIcon = [UIImage imageNamed: [[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleIconFiles"] objectAtIndex:0]];
    

    The UIImage automatically selects the suitable version (normal, @2x, ~ipad, ~ipad@2x or any future suffix). (BTW, don't you just hate this stupid scheme for identifying versions of images? It was just getting out of hand. Thank god for Asset Catalogues!)

    0 讨论(0)
提交回复
热议问题