如何向我的 Xcode 项目添加图标?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14347985/
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 do I add an icon to my Xcode project?
提问by Josias Viskoo
I have an Xcode project for Mac OSX, and I'd like to add an icon file (.icns) to my project and set it as the App Icon. How would I do this?
我有一个适用于 Mac OSX 的 Xcode 项目,我想向我的项目添加一个图标文件 (.icns) 并将其设置为应用程序图标。我该怎么做?
回答by abarnert
The key here is that you need an ICNS (icon set) file.
这里的关键是您需要一个 ICNS(图标集)文件。
The right way to create this is described in the Human Interface Guidelines, and the details are covered in various user's guides and reference guides in Apple's maze of twisty little documentation, but I'll summarize it here.
人机界面指南 中描述了创建它的正确方法,详细信息包含在 Apple 曲折的小文档迷宫中的各种用户指南和参考指南中,但我将在此处进行总结。
There are third-party tools that can do this, as well as plugins for Photoshop, GIMP, Illustrator, etc., that can output a correct .icns file. But make sure, if you use such a thing, it's up to date, because Apple changes the rules all the time.
有第三方工具可以做到这一点,还有适用于 Photoshop、GIMP、Illustrator 等的插件,可以输出正确的 .icns 文件。但是请确保,如果您使用这样的东西,它是最新的,因为 Apple 一直在更改规则。
If you need to do it manually, here's what you do:
如果您需要手动执行此操作,请执行以下操作:
First, create a set of PNG files at different sizes. The exact list of what you need changes over time. See Provide the Correct Resources and Let OS X Do the Workif that link lasts longer than the current list; otherwise, search for it at http://developer.apple.comyourself. But, as of early 2013, it's 512x512, 256x256, 128x128, 32x32, 16x16, and @2x versions of each. All of them should have the sRGB color profile embedded in them. They should be named either [email protected]
, icon_512x512.png
, etc., or [email protected]
, MyApp_512x512.png
, etc. Put them all in a directory together named, e.g., MyApp.iconset
.
首先,创建一组不同大小的 PNG 文件。您需要的确切列表会随着时间的推移而变化。如果该链接的持续时间比当前列表长,请参阅提供正确的资源并让 OS X 完成工作;否则,请自行在http://developer.apple.com 上搜索。但是,截至 2013 年初,它们分别是 512x512、256x256、128x128、32x32、16x16 和 @2x 版本。所有这些都应该嵌入 sRGB 颜色配置文件。它们应该命名为[email protected]
,icon_512x512.png
等,或[email protected]
,MyApp_512x512.png
等。将它们全部放在一个名为 的目录中,例如,MyApp.iconset
。
"But wait! I just want an icon, I don't want all those sizes!" Well, you really dowant all those sizes. Your 512x512 icon will look horrible when scaled down to 32x32. And on a Retina Mac, when your icon gets scaled up to double resolution, instead of getting sharper it'll just get jagged. Also, if you want to get into the App Store, Apple will reject you if you don't have them. But, if you insist, you can get away with just putting icon-512x512.png
in the folder, and follow the rest of the steps, and it will work.
“但是等等!我只想要一个图标,我不想要所有这些尺寸!” 嗯,你真的不希望所有的大小。当缩小到 32x32 时,您的 512x512 图标会看起来很糟糕。在 Retina Mac 上,当您的图标放大到双倍分辨率时,它只会变得锯齿状,而不是变得更清晰。另外,如果你想进入 App Store,如果你没有它们,Apple 会拒绝你。但是,如果您坚持,只需放入icon-512x512.png
文件夹,然后按照其余步骤操作即可,它会起作用的。
From the Terminal, cd into the parent directory, and type iconutil -c icns MyApp.iconset
. You will get a file called MyApp.icns
.
从终端, cd 进入父目录,然后键入iconutil -c icns MyApp.iconset
. 你会得到一个名为MyApp.icns
.
Now you can do the steps suggested by Douglas, and it will actually work. In Xcode, select your project in the Project Navigator, select your app target in the project sidebar, select the Summary tab, and drag MyApp.icns
from Finder to the App Icon box.
现在您可以执行 Douglas 建议的步骤,它实际上会起作用。在 Xcode 中,在项目导航器中选择您的项目,在项目侧栏中选择您的应用程序目标,选择摘要选项卡,然后MyApp.icns
从 Finder拖到应用程序图标框。
This may not have any visible effect in the GUI, except to add MyApp.icns
to the Project Navigator. In other words, you may still see the "?" icon. This seems to be a bug in Xcode 4.5. If you follow the out-of-date recommendations from the HIG two versions ago, Xcode always shows the icon, but if you follow the current HIG, it doesn't. Go figure. Hopefully Apple will fix that some day.
这在 GUI 中可能没有任何可见的效果,除了添加MyApp.icns
到项目导航器中。换句话说,您可能仍会看到“?” 图标。这似乎是 Xcode 4.5 中的一个错误。如果您遵循 HIG 两个版本前的过时建议,Xcode 始终会显示图标,但如果您遵循当前的 HIG,则不会。去搞清楚。希望苹果有一天能解决这个问题。
But for now, it doesn't matter. Build the project, and then look at MyApp.app
, and it will have your icon in the Finder, on the Dock, etc.
但现在,这并不重要。构建项目,然后查看MyApp.app
,它会在 Finder、Dock 等中显示您的图标。
Now, I know you don't want to draw the same picture in 10 different variations, you just want something simple. As long as you don't want to get into the App Store, you can get away with cheating, in two ways:
现在,我知道您不想以 10 种不同的变体绘制同一张图片,您只想要简单的东西。只要你不想进入App Store,你可以通过两种方式摆脱作弊:
- Scale the 512x512 (1024x1024 pixel "512x512@2x" if supporting hi res) image to all of the other sizes, using your favorite tool.
- Create a .icns with nothing but the 512x512 image in the iconset.
- 使用您喜欢的工具将 512x512(1024x1024 像素“512x512@2x”,如果支持高分辨率)图像缩放到所有其他尺寸。
- 创建一个 .icns ,除了图标集中的 512x512 图像之外什么都没有。
The second one is simpler, and less cheat-y, and ultimately Finder is probably going to scale your 512x512 image as well as you would have anyway.
第二个更简单,更少作弊,最终 Finder 可能会像无论如何一样缩放您的 512x512 图像。
Finally, if you've manually edited your Info.plist
or changed build settings (or you're using a project imported from a much easier version of Xcode), just dragging the image may not be enough. If you need to do the same steps manually, here they are:
最后,如果您手动编辑Info.plist
或更改了构建设置(或者您正在使用从更简单的 Xcode 版本导入的项目),仅拖动图像可能还不够。如果您需要手动执行相同的步骤,它们是:
MyApp.icns
has to be in the Project Navigator as a file in your project. (You can drag it here from Finder.)- In Build Phases, the Copy Bundle Resources should include
MyApp.icns
. (You can drag it here from the Project Navigator.) (If you're not using the normal Build Phases for some reason, you need some other way to get it copied toContents/Resources/MyApp.icns
at build time.) - Your Info.plist should have an
Icon file
(raw nameCFBundleIconFile
) namedMyApp
, with no extension.
MyApp.icns
必须在项目导航器中作为项目中的文件。(您可以从 Finder 将其拖到此处。)- 在构建阶段,复制捆绑资源应包括
MyApp.icns
. (您可以将它从 Project Navigator 拖到这里。)(如果您出于某种原因没有使用正常的构建阶段,则需要通过其他方式Contents/Resources/MyApp.icns
在构建时将其复制到。) - 您的 Info.plist 应该有一个名为的
Icon file
(原始名称CFBundleIconFile
)MyApp
,没有扩展名。
That's all there is to it.
这里的所有都是它的。
回答by iPhoneDev
In the project navigator, select an asset catalog.
Choose Editor > Add Assets > App Icons & Launch Images > New OS X Icon.
An empty OS X icon set is created, with an image well for each image representation in the set.
- Drag icon file from the Finder to the appropriate image well in the set viewer.
在项目导航器中,选择一个资产目录。
选择 Editor > Add Assets > App Icons & Launch Images > New OS X Icon。
创建了一个空的 OS X 图标集,其中每个图像表示都有一个图像井。
- 将图标文件从 Finder 拖到集合查看器中的相应图像井中。
Alternatively, you can add images by selecting an asset catalog, and choosing Editor > Add Assets > Import.
或者,您可以通过选择资产目录并选择编辑器 > 添加资产 > 导入来添加图像。
回答by Douglas
I am pretty sure you will have to put your icons into the project and then recompile. Let us know how it goes.
我很确定您必须将图标放入项目中,然后重新编译。让我们知道怎么回事。