如何在 Xcode 中为 Mac 应用程序设置图标?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6337787/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I set the icon for a Mac Application in Xcode?
提问by kmikael
I have been learning a lot about writing Objective-C code and designing in Interface Builder and I wanted to set icons for my simple programs.
我已经学习了很多关于在 Interface Builder 中编写 Objective-C 代码和设计的知识,我想为我的简单程序设置图标。
I added the same JPG to all the size fields in Icon Composer and got an ICNS, but I couldn't figure out how to add it to the project.
我在 Icon Composer 中的所有大小字段中添加了相同的 JPG 并获得了一个 ICNS,但我不知道如何将它添加到项目中。
Thank you in advance.
先感谢您。
回答by codingFriend1
Since Xcode 4.4 Icon Composer is no longer the recommended way to create icons and is no longer included in the standard install of Xcode. Due to the introduction of Macs with retina display, it is now recommended to provide high resolution versions of all graphics including app icons.
由于 Xcode 4.4 Icon Composer 不再是创建图标的推荐方式,并且不再包含在 Xcode 的标准安装中。由于引入了带有视网膜显示的 Mac,现在建议提供所有图形的高分辨率版本,包括应用程序图标。
To give your app an icon under Xcode > 4.4 do the following:
要在 Xcode > 4.4 下为您的应用程序提供一个图标,请执行以下操作:
Create a folder [IconName].iconset in Finder
In this folder place your icon as png files. You'll need the icon in sizes of 16px, 32px, 64px (retina only), 128px, 256px, 512px and 1024px (retina only)
These icons must be named with the pattern icon_16x16.png, icon_32x32.png, icon_128x128.png and so on
To support retina displays you must also add icon files with double resolution, named [email protected] (with size 32x32), [email protected] (size 64x64) and so on up to [email protected] (size 1024x1024).
drag this [IconName].iconset folder to Xcode (copy if necessary)
in the info.plist file set the "CFBundleIconFile" (Icon File as Key) value to [IconName] but without the .iconset extension
在 Finder 中创建文件夹 [IconName].iconset
在此文件夹中,将您的图标放置为 png 文件。您需要大小为 16 像素、32 像素、64 像素(仅限视网膜)、128 像素、256 像素、512 像素和 1024 像素(仅限视网膜)的图标
这些图标必须以 icon_16x16.png、icon_32x32.png、icon_128x128.png 等模式命名
要支持视网膜显示,您还必须添加双分辨率的图标文件,命名为 [email protected](大小为 32x32)、[email protected](大小为 64x64)等等,直到 [email protected](大小为 1024x1024)。
将此 [IconName].iconset 文件夹拖到 Xcode(如有必要,请复制)
在 info.plist 文件中将“CFBundleIconFile”(图标文件作为键)值设置为 [IconName] 但没有 .iconset 扩展名
Annotations:
注释:
- it is (currently) not required to provide the @2x icons
- it will (usually) also work if you don't provide every icon file
- the iconset folder should not contain a icon_64x64.png file. the 64px icon is only for the retina version of the icon_32x32
- (当前)不需要提供@2x 图标
- 如果您不提供每个图标文件,它(通常)也会起作用
- iconset 文件夹不应包含 icon_64x64.png 文件。64px 图标仅适用于 icon_32x32 的视网膜版本
Update:In the end your .iconset folder has the following 10 items:
更新:最后你的 .iconset 文件夹有以下 10 个项目:
icon_16x16.png
[email protected]
icon_32x32.png
[email protected]
icon_128x128.png
[email protected]
icon_256x256.png
[email protected]
icon_512x512.png
[email protected]
Official guide:
官方指南:
Additional information:
附加信息:
To convert the iconset folder to an icns file, run the following command on the terminal:
要将 iconset 文件夹转换为 icns 文件,请在终端上运行以下命令:
iconutil -c icns [IconName].iconset
where [IconName] should be replaced with the prefix of the iconset folder. You now have a file called [IconName].icns. In Xcode 4.4, in the Target Summary, right click the question mark for the icon, then select the icns file. You should then see the question mark get replaced with the icon.
其中 [IconName] 应替换为图标集文件夹的前缀。您现在有一个名为 [IconName].icns 的文件。在 Xcode 4.4 中,在 Target Summary 中,右键单击图标的问号,然后选择 icns 文件。然后您应该会看到问号被替换为图标。
回答by Senseful
As of Xcode 7 (not sure when this was originally introduced), you can use the Assets.xcassetsfile for app icons. This file is included by default for new projects.
从 Xcode 7(不确定最初何时引入)开始,您可以将Assets.xcassets文件用于应用程序图标。新项目默认包含此文件。
Simply:
简单地:
- Go to your target's settings and ensure that under General, App Iconsthe Sourceis set to AppIcon.
- Add all 10 png icons to your Assets.xcassets's AppIcon image.
- Delete derived dataif you have launched the app before, otherwise it will continue to show the default icon.
- Run the app; it should now show your icon in the Dock, Application Switcher, and anywhere else it should appear.
- 转到目标的设置并确保在General、App Icons 下将Source设置为AppIcon。
- 将所有 10 个 png 图标添加到 Assets.xcassets 的 AppIcon 图像中。
- 如果您之前启动过该应用程序,请删除派生数据,否则它将继续显示默认图标。
- 运行应用程序;它现在应该在 Dock、应用程序切换器和它应该出现的任何其他地方显示您的图标。
回答by Senseful
Follow these stepsto add an application icon to your project. This is the icon that will be show by the MacOS in the doc bar and alt-tab display.
按照以下步骤将应用程序图标添加到您的项目中。这是 MacOS 将在文档栏和 alt-tab 显示中显示的图标。
- Create an icon .icns resource file
- Place it in the "resources/macos" folder
- Add it to the resources group in the xcode project
- Edit the Info.plist file and change the "CFBundleIconFile" value string to "icon"
- 创建图标 .icns 资源文件
- 将其放在“resources/macos”文件夹中
- 将其添加到 xcode 项目中的资源组
- 编辑 Info.plist 文件并将“CFBundleIconFile”值字符串更改为“icon”
Also, img2icnstool can come handy to convert images to an icon.
此外,img2icns工具可以方便地将图像转换为图标。
回答by Arun
Steps to add an application icon to your cocoa project.
将应用程序图标添加到可可项目的步骤。
- Find 'Icon Composer' from spotlight.
- Drag and drop the icon(name should be like imagename.icns) in the given boxes.
- Select one box and save it.
- Drag and drop the saved image in the Resources folder of your application.
- Select target-->right click on project name-->Select GetInfo.
- In Properties enter the name of 'Icon File'.
- Now Clean Build and run your application.
- 从聚光灯下找到“图标编辑器”。
- 将图标(名称应类似于 imagename.icns)拖放到给定的框中。
- 选择一个框并保存。
- 将保存的图像拖放到应用程序的 Resources 文件夹中。
- 选择目标-->右键单击项目名称-->选择GetInfo。
- 在属性中输入“图标文件”的名称。
- 现在 Clean Build 并运行您的应用程序。
回答by Alexandr
It's easy
这很简单
Use automatic tool (Such as IconFly) for create correct ICNS or iconset with all necessary size.
Then drag and drop created ICNS or iconset to Xcode.
in the info.plist file set the "CFBundleIconFile" value to [IconName]
使用自动工具(例如 IconFly)创建具有所有必要大小的正确 ICNS 或图标集。
然后将创建的 ICNS 或图标集拖放到 Xcode。
在 info.plist 文件中将“CFBundleIconFile”值设置为 [IconName]
回答by eonist
Xcode 8.2.1
Xcode 8.2.1
Convert .png .icns via IconMaker
Add the AwesomeApp.icns file in the same folder as info.plist
Drag AwesomeApp.icns file into xcode in the same folder as info.plist
In info.plist set icon: to AwesomeApp.icns
通过IconMaker转换 .png .icns
在 info.plist 所在的文件夹中添加 AwesomeApp.icns 文件
将 AwesomeApp.icns 文件拖入与 info.plist 相同的文件夹中的 xcode
在 info.plist 设置图标:到 AwesomeApp.icns
回答by Rachit kapadia
Step 1: Get iconfile name from info.plist and place icns(icon file) folder in SourceCode/resources folder
步骤 1:从 info.plist 获取 iconfile 名称并将 icns(icon file) 文件夹放在 SourceCode/resources 文件夹中
Step 2: And same thing goes for xcode,you have to copy all images from xcode using copyallframeworks or copyall resource file in your xcode and build the app again.
第 2 步:xcode 也是如此,您必须使用 xcode 中的 copyallframeworks 或 copyall 资源文件从 xcode 复制所有图像,然后再次构建应用程序。