macos 如何为我的应用程序的 Mac OS X 应用程序包设置图标?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/646671/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 06:04:24  来源:igfitidea点击:

How do I set the icon for my application's Mac OS X app bundle?

macos

提问by Walt D

I have an application that I've bundled into a Mac OS X app bundle. Everything is working fine, but I want to change its icon from the default. How do I set its icon? Thanks.

我有一个应用程序已捆绑到 Mac OS X 应用程序包中。一切正常,但我想更改默认图标。我如何设置它的图标?谢谢。

回答by cobbal

in your info.plistadd

在您info.plist添加

<key>CFBundleIconFile</key>
<string>iconfile</string>

with icon file iconfile.icnsin your Resources directory

带有iconfile.icns资源目录中的图标文件

回答by sage

If you came here because you have a single app and want to change the image on your computer only (not sure how it works for sharing), there are much easier ways. In particular, here are two options I have used:

如果您来到这里是因为您只有一个应用程序并且只想更改计算机上的图像(不确定它如何用于共享),还有更简单的方法。特别是,这里有两个我使用过的选项:

  1. If you want to copy an existing icon:

    • Select the source item and press Cmd-I (Apple-I)
    • Select the item you want to change and press Cmd-I (Apple-I)
    • Drag the icon from the source to the top left icon of the one you want to change (the example image shows the target icon: it is the 'folder' icon to the left of the words "bird_id 2"): enter image description here
  2. Create a .icns file from any image. If you use MacPorts, I recommend instead using the port makeicns - see below for more info. You can alternatively do this using an app such as http://www.img2icnsapp.com/as recommended at https://discussions.apple.com/thread/2773825.

  1. 如果要复制现有图标:

    • 选择源项目并按 Cmd-I (Apple-I)
    • 选择要更改的项目并按 Cmd-I (Apple-I)
    • 将图标从源图标拖到要更改的图标的左上角图标(示例图像显示目标图标:它是单词“bird_id 2”左侧的“文件夹”图标): 在此处输入图片说明
  2. 从任何图像创建一个 .icns 文件。如果您使用 MacPorts,我建议您改为使用端口 makeicns - 有关更多信息,请参见下文。您也可以按照https://discussions.apple.com/thread/2773825 上的建议使用http://www.img2icnsapp.com/等应用程序执行此操作。

makeicns v1.4.10 (284bd686824f)

Usage: makeicns [k1=v1] [k2=v2] ...

Keys and values include:
    512: Name of input image for 512x512 variant of icon
    256: Name of input image for 256x256 variant of icon
    128: Name of input image for 128x128 variant of icon
     32: Name of input image for 32x32 variant of icon
     16: Name of input image for 16x16 variant of icon
     in: Name of input image for all variants not having an explicit name
    out: Name of output file, defaults to first nonempty input name,
         but with icns extension

  align: [center, left, right, top, bottom] {First letter suffices!}

Examples:

  makeicns -512 image.png -32 image.png
      Creates image.icns with only a 512x512 and a 32x32 variant.

  makeicns -in myfile.jpg -32 otherfile.png -out outfile.icns
      Creates outfile.icns with sizes 512, 256, 128, and 16 containing data
      from myfile.jpg and with size 32 containing data from otherfile.png.

回答by Alexandre Strube

I made a small script that takes a big image and resizes it to all expected icon sizes for Mac OS, including the double ones for retina displays. It takes the original png file, which I expect to be as big as the maximum size, if not bigger, to make sure they are rendered at maximum quality.

我制作了一个小脚本,用于获取大图像并将其调整为 Mac OS 的所有预期图标大小,包括用于视网膜显示器的双倍大小。它需要原始 png 文件,我希望它与最大尺寸一样大,如果不是更大的话,以确保它们以最高质量呈现。

It resizes and copies them to a icon set, and uses the Mac OS's 'iconutil' tool to join them into a .icns file.

它调整大小并将它们复制到一个图标集,并使用 Mac OS 的“iconutil”工具将它们连接到一个 .icns 文件中。

For this script to run, you need your original icon file to be a png, and you have your bundle in more or less working order. You only need to touch the first three lines.

要运行此脚本,您需要将原始图标文件设为 png,并且您的包或多或少处于工作状态。您只需要触摸前三行。

export PROJECT=Myproject
export ICONDIR=$PROJECT.app/Contents/Resources/$PROJECT.iconset
export ORIGICON=Mybigfile.png

mkdir $ICONDIR

# Normal screen icons
for SIZE in 16 32 64 128 256 512; do
sips -z $SIZE $SIZE $ORIGICON --out $ICONDIR/icon_${SIZE}x${SIZE}.png ;
done

# Retina display icons
for SIZE in 32 64 256 512; do
sips -z $SIZE $SIZE $ORIGICON --out $ICONDIR/icon_$(expr $SIZE / 2)x$(expr $SIZE / 2)x2.png ;
done

# Make a multi-resolution Icon
iconutil -c icns -o $PROJECT.app/Contents/Resources/$PROJECT.icns $ICONDIR
rm -rf $ICONDIR #it is useless now