23

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 they all return nil:

image = [UIImage imageNamed:@"AppIcon"];
image = [UIImage imageNamed:@"icon"];
image = [UIImage imageNamed:@"icon-76"];
image = [UIImage imageNamed:@"icon-60"];

Other images in the assets catalog work as expected.

hpique
  • 119,096
  • 131
  • 338
  • 476

3 Answers3

23

By inspecting the bundle I found that the icon images were renamed as:

AppIcon76x76~ipad.png
AppIcon76x76@2x~ipad.png
AppIcon60x60@2x.png

And so on.

Thus, using [UIImage imageNamed:@"AppIcon76x76"] or similar works.

Is this documented somewhere?

hpique
  • 119,096
  • 131
  • 338
  • 476
  • 1
    There seems to be documentation here: https://developer.apple.com/library/ios/qa/qa1686/_index.html – Peter K. Nov 20 '14 at 15:55
  • 1
    This is dangerous to do. Apple might change the naming convention and your app break in future. My suggestion is just to copy another icon in your image.xcasset separately. – GeneCode Dec 02 '16 at 02:54
  • is there a method to get the higher resolution images? – themenace Jul 13 '20 at 12:56
18

I recommend retrieving the icon URL by inspecting the Info.plist since there's no guarantee how the Icon files are named:

NSDictionary *infoPlist = [[NSBundle mainBundle] infoDictionary];
NSString *icon = [[infoPlist valueForKeyPath:@"CFBundleIcons.CFBundlePrimaryIcon.CFBundleIconFiles"] lastObject];
imageView.image = [UIImage imageNamed:icon]; 

In this case we're fetching the last image URL of the CFBundleIconFiles array. It has the largest resolution. Change this if you need a smaller resolution.

Ortwin Gentz
  • 52,648
  • 24
  • 135
  • 213
4

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)
Alberto Malagoli
  • 1,173
  • 10
  • 14