如何通过脚本快速生成指定尺寸与命名的多份应用图标图片?
Absolutely! You can quickly generate all 8 required icon sizes with a shell script—ImageMagick is the best cross-platform option (works seamlessly on Mac), and if you prefer no extra installs, Mac's built-in sips tool works too (with a small caveat for vector files). Here's how to do both:
Option 1: ImageMagick (Best for .ai/.svg/.png)
ImageMagick handles vector formats directly and produces crisp resized outputs, making it ideal for your vector source file.
Step 1: Install ImageMagick
On Mac, use Homebrew (if you don't have Homebrew, install it first with /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"):
brew install imagemagick
Step 2: Create the Script
Save this as generate_icons.sh:
#!/bin/bash # Replace this with your source file path (.ai, .svg, or .png) SOURCE_IMAGE="your_source_icon.ai" # List of required icons: "filename.png widthxheight" ICON_SPECS=( "AppIconMask@3x~ipad.png 180x180" "AppIconMask@3x~iphone.png 180x180" "AppIconMask@2x~ipad.png 152x152" "AppIconMask@2x~iphone.png 120x120" "GameAppIconMask@2x.png 84x84" "SpotlightAppIconMask@2x.png 80x80" "TableIconMask@2x.png 58x58" "NotificationAppIconMask@2x.png 40x40" ) # Generate each icon for spec in "${ICON_SPECS[@]}"; do FILENAME=$(echo "$spec" | awk '{print $1}') SIZE=$(echo "$spec" | awk '{print $2}') echo "Generating $FILENAME ($SIZE)..." # Resize, center-align, and ensure exact dimensions (handles non-square sources if needed) convert "$SOURCE_IMAGE" -resize "$SIZE" -gravity center -extent "$SIZE" "$FILENAME" done echo "✅ All icons generated successfully!"
Step 3: Run the Script
- Update
SOURCE_IMAGEto point to your actual design file. - Make the script executable:
chmod +x generate_icons.sh - Run it:
./generate_icons.sh
Option 2: Mac's Built-in sips (No Extra Installs)
Sips comes pre-installed on Mac, but it has limited support for .ai/.svg. For vector files, first export a high-resolution PNG (e.g., 1024x1024) from your design tool, then use this script:
Save as generate_icons_sips.sh:
#!/bin/bash # Replace with your high-res source PNG path SOURCE_IMAGE="your_high_res_icon.png" # List of required icons: "filename.png width height" ICON_SPECS=( "AppIconMask@3x~ipad.png 180 180" "AppIconMask@3x~iphone.png 180 180" "AppIconMask@2x~ipad.png 152 152" "AppIconMask@2x~iphone.png 120 120" "GameAppIconMask@2x.png 84 84" "SpotlightAppIconMask@2x.png 80 80" "TableIconMask@2x.png 58 58" "NotificationAppIconMask@2x.png 40 40" ) # Generate each icon for spec in "${ICON_SPECS[@]}"; do FILENAME=$(echo "$spec" | awk '{print $1}') WIDTH=$(echo "$spec" | awk '{print $2}') HEIGHT=$(echo "$spec" | awk '{print $3}') echo "Generating $FILENAME ($WIDTHx$HEIGHT)..." sips -z "$HEIGHT" "$WIDTH" "$SOURCE_IMAGE" --out "$FILENAME" done echo "✅ Done!"
Run it the same way as the ImageMagick script—update the source path, make it executable, and execute.
内容的提问来源于stack exchange,提问作者burntscarr

