如何获取ios中的当前应用程序图标

有没有办法在cocoa触摸应用程序中获取当前的应用程序图标? 谢谢。

我完全基于moosgummi的回答,而是返回一个UIImage

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

UIImage自动select合适的版本(正常, @2x~ipad~ipad@2x或任何未来的后缀)。 (顺便说一下,你不是只讨厌这个用于识别图像版本的愚蠢的scheme吗?它只是失控而已,感谢上帝的资产目录!)

接受的答案不适合我,我使用Xcode 5的Images.xcassets存储应用程序图标的方法。 这个修改为我工作:

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

如果有疑问,只需使用lldb探索主包的infoDictionary。

这是如何在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; }; }; 

如果你想获得应用程序图标使用下面的代码:

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

PS:UIImage自动为视网膜显示select合适的图标。

我希望它可以帮助!

这很容易,因为当前图标的文件名在Info.plist中设置:

 NSString *filePath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleIconFiles"] objectAtIndex:0]]; 

如果你想select高分辨率的版本,你应该使用:

 NSString *filePath = nil; for (NSString *fileName in [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleIconFiles"]) { if ([fileName rangeOfString:@"@2x"].location != NSNotFound) { filePath = [[NSString alloc] initWithString:fileName]; } } 

如果您使用资产目录,那么您可以简单地使用:

UIImage* appIcon = [UIImage imageNamed:@"AppIcon60x60"];

资产目录似乎标准化了应用程序图标的文件名。

当然,如果你需要列出你的包中的所有应用程序图标,你可以在信息字典的CFBundleIconFiles分支中查找它。

使用KVC,你可以很容易地在一行代码中获得:

 NSArray* allIcons = [[[NSBundle mainBundle] infoDictionary] valueForKeyPath:@"CFBundleIcons.CFBundlePrimaryIcon.CFBundleIconFiles"]; 

生成的数组将包含您的包中可用的所有应用程序图标名称。 任何这些名称都可以用来检索图标使用+[UIImage imageNamed:]

 <__NSCFArray 0x15d54260>( AppIcon29x29, AppIcon40x40, AppIcon60x60 ) 

因为花了我一点时间才解决,这是一个可行的Swift解决scheme。

  let primaryIconsDictionary = NSBundle.mainBundle().infoDictionary?["CFBundleIcons"]?["CFBundlePrimaryIcon"] as? NSDictionary let iconFiles = primaryIconsDictionary!["CFBundleIconFiles"] as NSArray let lastIcon = iconFiles.lastObject as NSString //last seems to be largest, use first for smallest let theIcon = UIImage(named: lastIcon) let iconImageView = UIImageView(image: theIcon) 

这是UIApplication获取应用程序图标的Swift 3.x扩展。 您可以根据从iconFiles数组中提取的图标path的位置来select是获取最小或最大的图标。

 extension UIApplication { var icon: UIImage? { guard let iconsDictionary = Bundle.main.infoDictionary?["CFBundleIcons"] as? NSDictionary, let primaryIconsDictionary = iconsDictionary["CFBundlePrimaryIcon"] as? NSDictionary, let iconFiles = primaryIconsDictionary["CFBundleIconFiles"] as? NSArray, // First will be smallest for the device class, last will be the largest for device class let lastIcon = iconFiles.lastObject as? String, let icon = UIImage(named: lastIcon) else { return nil } return icon } } 

要访问该图标,请调用以下内容:

 let icon = UIApplication.shared.icon 

对于奖励积分,如果你的应用程序需要,你甚至可以做两个增值服务来获得最小和最大的图标。

如果你使用Xamarin:

 var iconNames = NSBundle.MainBundle.InfoDictionary.ValueForKeyPath((NSString)"CFBundleIcons.CFBundlePrimaryIcon.CFBundleIconFiles") as NSArray; var name = iconNames.GetItem<NSString>(iconNames.Count - 1); //Grabs last item, call with 0 for the first item var icon = UIImage.FromBundle(name); 

根据安迪的答案使用KVC方法。 比特冗长,但是使用KeyPath比链接对ValueForKey的调用要好。