Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get UIImage of AppIcon?

I am using Assets Catalog, and adding app icon of various size to the assets is okay.

But when I tried to get the UIImage programmatically, it returned nil.

UIImage *appIcon = [UIImage imageNamed"AppIcon"]; 

The app icon is named AppIcon (the default), and the actual files are app-icon-256.png etc.

I also noticed that unlike normal images, app icons are in an "App Icon" set, in the directory AppIcon.appiconset. Normal images are in the directory someimage.imageset.

like image 854
samwize Avatar asked Apr 02 '14 10:04

samwize


People also ask

How do you add a UIImage in Objective C?

UIImage *img = [[UIImage alloc] init]; [img setImage:[UIImage imageNamed:@"anyImageName"]];

What is the difference between UIImage and UIImageView?

UIImage contains the data for an image. UIImageView is a custom view meant to display the UIImage .


2 Answers

The problem is that the AppIcon from the asset catalog is not placed in the the catalog after compiling. Instated it it copied into your apps bundle, just like before.

The name conversion used when copying the icon to the app bundle is AppIcon<size>.png, where the size is for example 40x40 or 72x72

You can get your apps icons by specifying the size of the app icon you want:

UIImage *appIcon = [UIImage imageNamed:@"AppIcon40x40"]; 
like image 162
rckoenes Avatar answered Sep 22 '22 12:09

rckoenes


The >iOS10 answer is:

Copy & paste this extension.

extension Bundle {      var icon: UIImage? {          if let icons = infoDictionary?["CFBundleIcons"] as? [String: Any],        let primary = icons["CFBundlePrimaryIcon"] as? [String: Any],        let files = primary["CFBundleIconFiles"] as? [String],        let icon = files.last     {       return UIImage(named: icon)     }          return nil   } } 

Then just call this:

Bundle.main.icon 

SwiftUI:

Image(uiImage: Bundle.main.icon ?? UIImage()) 
like image 36
Rufat Mirza Avatar answered Sep 22 '22 12:09

Rufat Mirza