How to manually create icns files using iconutil?

2020-01-26 12:40发布

问题:

When I'm validating my app I get this error:

the application bundle does not contain an icon in ICNS format, containing both a 512x512 and a 512x512@2x image.

I use to make the icns icons with Img2icns app and until today it always worked properly. But now I'm getting that error and there's no way to make it work. I tried to put two PNG files together (512x512 and 1024x1024) in Img2icns but I always get that error. I also tried to follow the instructions in Apple's OS X Human Interface Guideline but when I try to make the icon sets I get this terminal error:

-bash: syntax error near unexpected token 'newline'

I am not very good with terminal commands so maybe I'm doing something wrong. I wrote:

iconutil -c icns </Users/myname/SDK Mac Apps/MyApp/grafica/icon.iconset>

If anyone could help it would be very much appreciated. Thanks, Massy.

回答1:

Checkout the following instructions (link):

Use iconutil to Create an icns File Manually

The iconutil command-line tool converts iconset folders to deployment-ready, high-resolution icns files. (You can find complete documentation for this tool by entering man iconutil in Terminal.) Using this tool also compresses the resulting icns file, so there is no need for you to perform additional compression.

To convert a set of icons to an icns file

Enter this command into the Terminal window:

iconutil -c icns <iconset filename>

where <iconset filename> is the path to the folder containing the set of icons you want to convert to icns. The output is written to the same location as the iconset file, unless you specify an output file as shown:

iconutil -c icns -o <icon filename> <iconset filename>

In other words, you need to replace <iconset filename> by the path:

/Users/myname/SDK Mac Apps/MyApp/grafica/icon.iconset

Since the path contains spaces, you need to use double quotes, for example:

iconutil -c icns "/Users/myname/SDK Mac Apps/MyApp/grafica/icon.iconset"

This command should work properly.



回答2:

Here's a script to convert a 1024x1024 png (named "Icon1024.png") to the required icns file. Save it to a filed called "CreateICNS.src" in the folder where your png file is then in terminal "cd" to the same folder and type "source CreateICNS.src" to call it:

mkdir MyIcon.iconset
sips -z 16 16     Icon1024.png --out MyIcon.iconset/icon_16x16.png
sips -z 32 32     Icon1024.png --out MyIcon.iconset/icon_16x16@2x.png
sips -z 32 32     Icon1024.png --out MyIcon.iconset/icon_32x32.png
sips -z 64 64     Icon1024.png --out MyIcon.iconset/icon_32x32@2x.png
sips -z 128 128   Icon1024.png --out MyIcon.iconset/icon_128x128.png
sips -z 256 256   Icon1024.png --out MyIcon.iconset/icon_128x128@2x.png
sips -z 256 256   Icon1024.png --out MyIcon.iconset/icon_256x256.png
sips -z 512 512   Icon1024.png --out MyIcon.iconset/icon_256x256@2x.png
sips -z 512 512   Icon1024.png --out MyIcon.iconset/icon_512x512.png
cp Icon1024.png MyIcon.iconset/icon_512x512@2x.png
iconutil -c icns MyIcon.iconset
rm -R MyIcon.iconset


回答3:

There's a command-line node module that does all the work of converting a PNG file into an iconset directory:

npm install -g node-icns
nicns --in adventure-cat.png --out adventure-cat.icns


回答4:

While using all kinds of scripts to convert hi-res PNG image to a pleiad of different low-resolution copies may seem handy (and it really is), one should not forget that this kind of automatic resizing will render perceptibly imperfect images.

The lesser the resolution, the blurrier the icon!

Instead, you should always request a logo in some vector format from your designer, for example in SVG. With this on hand, you can manually prepare perfect PNG files in all required resolutions and then make a single .icns file, which will make your app icon look beautiful on every single screen, from mobile phone to some high-end Retina display of the latest iMac.

From the latest Apple guideline as of 2016, you should prepare the following PNG files:

+---------------------+--------------------+--------------+
|      filename       | resolution, pixels | density, PPI |
+---------------------+--------------------+--------------+
| icon_16x16.png      | 16x16              |           72 |
| icon_16x16@2x.png   | 32x32              |          144 |
| icon_32x32.png      | 32x32              |           72 |
| icon_32x32@2x.png   | 64x64              |          144 |
| icon_128x128.png    | 128x128            |           72 |
| icon_128x128@2x.png | 256x256            |          144 |
| icon_256x256.png    | 256x256            |           72 |
| icon_256x256@2x.png | 512x512            |          144 |
| icon_512x512.png    | 512x512            |           72 |
| icon_512x512@2x.png | 1024x1024          |          144 |
+---------------------+--------------------+--------------+

After all the PNG files are prepared, place them into some directory with .iconset extension (Logos.iconset for example) and execute the following from the Terminal:

iconutil --convert icns Logos.iconset

If there were no errors after executing this command, then all the files were processed properly, and you got the Logos.icns file in the same directory, containing all the beautiful crisp logos for your app which will suit any modern screen as of 2017.



回答5:

These commands (entered in Terminal) worked for me to convert an old icns file to the new format:

cd Folder_With_Icns_File
iconutil -c iconset Your_Icon_Name.icns 
rm Your_Icon_Name.icns 
iconutil -c icns Your_Icon_Name.iconset
rm -R Your_Icon_Name.iconset

Update

The -c parameter to iconutil is no longer supported. Use --convert instead:

cd Folder_With_Icns_File
iconutil --convert iconset Your_Icon_Name.icns 
rm Your_Icon_Name.icns 
iconutil --convert icns Your_Icon_Name.iconset
rm -R Your_Icon_Name.iconset


回答6:

Additional comment, when you create .icns file, you need to rename all the pic files with prefix "icon_", otherwise, iconutil will fail with error message: ".iconset:error: Failed to generate ICNS." which is not informative at all.



回答7:

I have written a bash script for making icns from a svg file:

#!/usr/bin/env bash 
sizes=(16 32 64 128 256 512)
largfile='icon_512x512@2x.png'
if [ ! -f "$largfile" ]; then
  convert -background none -resize 1024x1024 "$1" "$largfile"
fi
for s in "${sizes[@]}"; do
  echo $s
  convert -background none -resize ${s}x${s} "$largfile" "icon_${s}x$s.png"
done

cp 'icon_32x32.png'     'icon_16x16@2x.png'
mv 'icon_64x64.png'     'icon_32x32@2x.png'
cp 'icon_256x256.png'   'icon_128x128@2x.png'
cp 'icon_512x512.png'   'icon_256x256@2x.png'

mkdir icon.iconset
mv icon_*x*.png icon.iconset
iconutil -c icns icon.iconset

Make sure you have imagemagick installed with librsvg support, on mac:

brew install imagemagick --with-librsvg

This script has served me quite well.


Update

For a more thorough treatment, I have created a command line tool (written in Swift) for generating AppIcon.appiconset with the correct layout and format:

https://github.com/kindlychung/genicon



回答8:

Same as @Henry (comment above) but takes as argument the PNG filename and outputs the ICNS with the same name.

NOTE : The PNG file name is only expected to have 1 point to separate extension, i.e. xpto.png .

So, save the code below to a filed called "CreateICNS.src" in the folder where your png file is.

CODE :

IFS='.' read -ra ADDR <<< "$1"
ICONSET=${ADDR[0]}.iconset
mkdir $ICONSET
sips -z 16 16     $1 --out $ICONSET/icon_16x16.png
sips -z 32 32     $1 --out $ICONSET/icon_16x16@2x.png
sips -z 32 32     $1 --out $ICONSET/icon_32x32.png
sips -z 64 64     $1 --out $ICONSET/icon_32x32@2x.png
sips -z 128 128   $1 --out $ICONSET/icon_128x128.png
sips -z 256 256   $1 --out $ICONSET/icon_128x128@2x.png
sips -z 256 256   $1 --out $ICONSET/icon_256x256.png
sips -z 512 512   $1 --out $ICONSET/icon_256x256@2x.png
sips -z 512 512   $1 --out $ICONSET/icon_512x512.png
cp $1 $ICONSET/icon_512x512@2x.png
iconutil -c icns $ICONSET
rm -R $ICONSET

HOW TO USE :

Then in the terminal, "cd" to the same folder and type :

source CreateICNS.src {PNG filename}

where {PNG filename} is the name of your PNG file, i.e. xpto.png .

If your file would be named abc.png you would use :

source CreateICNS.src abc.png


回答9:

I've refactored @Henry's script to make it look better:

#!/bin/zsh
NAME=$(basename $1 .png); DIR="$NAME.iconset"
mkdir -pv $DIR
for m r in 'n' '' '((n+1))' '@2x'; do
    for n in $(seq 4 9 | grep -v 6); do
        p=$((2**$m)); q=$((2**$n))
        OUT="$DIR/icon_${q}x${q}${r}.png"
        sips -z $p $p $1 --out $OUT
    done
done
iconutil -c icns $DIR
rm -frv $DIR

Update

The -c parameter to iconutil is no longer supported. Use -—convert instead:

#!/bin/zsh
NAME=$(basename $1 .png); DIR="$NAME.iconset"
mkdir -pv $DIR
for m r in 'n' '' '((n+1))' '@2x'; do
    for n in $(seq 4 9 | grep -v 6); do
        p=$((2**$m)); q=$((2**$n))
        OUT="$DIR/icon_${q}x${q}${r}.png"
        sips -z $p $p $1 --out $OUT
    done
done
iconutil -—convert icns $DIR
rm -frv $DIR


回答10:

Dead simple .png