tauri开发中,使用node将png图片转成苹果的icns图标格式,解决tauri icon生成的mac图标过大问题

在tauri开发中,我们使用tauri icon生成的图标在windows上是正常的,但是在mac上就显示过大,也可以看tauri的issue:[v2]When using the Tauri Icon to generate icons, it is always larger than other icons in Mac · tauri-apps/tauri · Discussion #10999 · GitHub

为了解决这个烦人的问题,有两种方式,一种是单独用ps给mac做一个图标,这种方式我也写过一个文章:创建一个和tauri图标一致的icon和苹果icns图标_tauri icon-CSDN博客。另外一种方式就是写一个js脚本,让自动执行生成一个icon图标。接下来就是展示的时间了:

1.在项目cli下创建一个creatIcon.cjs脚本文件

安装依赖:

javascript 复制代码
pnpm install sharp png2icons -D

创建脚本:

javascript 复制代码
const fs = require('fs')
const path = require('path')
const sharp = require('sharp')
const png2icons = require('png2icons')

// 输入 PNG 文件路径
const inputPath = path.join(__dirname, './app.png')
// const inputPath = path.join(__dirname, './pakeplusicon.png')
// 临时输出带圆角和 padding 的 PNG 文件路径
const processedOutputPath = path.join(__dirname, './processed-image.png')
// 输出 ICNS 文件路径
const icnsOutputPath = path.join(__dirname, '../src-tauri/icons/icon.icns')

// 给图片添加圆角并添加 padding
sharp(inputPath)
    .resize({
        // 确保图片尺寸一致
        width: 1024,
        height: 1024,
        fit: 'contain',
        background: { r: 0, g: 0, b: 0, alpha: 0 },
    })
    .composite([
        // rx ry是圆角半径
        {
            input: Buffer.from(
                `<svg>
         <rect x="0" y="0" width="1024" height="1024" rx="250" ry="250" />
       </svg>`
            ),
            blend: 'dest-in',
        },
    ])
    // top/bottom/left/right 是 padding
    .extend({
        top: 120,
        bottom: 120,
        left: 120,
        right: 120,
        background: { r: 0, g: 0, b: 0, alpha: 0 },
    })
    .toFile(processedOutputPath)
    .then(() => {
        console.log(
            'Image processing complete with rounded corners and padding.'
        )

        // 读取处理后的 PNG 文件
        fs.readFile(processedOutputPath, (err, data) => {
            if (err) {
                console.error('Error reading processed PNG file:', err)
                return
            }

            // 转换 PNG 到 ICNS 格式
            const icnsBuffer = png2icons.createICNS(data, 1, 0)
            if (icnsBuffer) {
                fs.writeFile(icnsOutputPath, icnsBuffer, (writeErr) => {
                    if (writeErr) {
                        console.error('Error writing ICNS file:', writeErr)
                    } else {
                        console.log(
                            'ICNS file created successfully:',
                            icnsOutputPath
                        )
                        // copy icns to /Users/song/Project/my/PakePlus/src-tauri/icons
                        fs.copyFile(
                            icnsOutputPath,
                            path.join(
                                __dirname,
                                '../src-tauri/icons/icon.icns'
                            ),
                            (copyErr) => {
                                if (copyErr) {
                                    console.error(
                                        'Error copying ICNS file:',
                                        copyErr
                                    )
                                } else {
                                    console.log(
                                        'ICNS file copied successfully.'
                                    )
                                }
                            }
                        )
                    }
                })
            } else {
                console.error('Failed to convert PNG to ICNS.')
            }
        })
    })
    .catch((err) => {
        console.error('Error during image processing:', err)
    })

项目结构:

将原始图片放在cli下的app.png,然后执行命令:

javascript 复制代码
node ./cli/creatIcon.cjs

最后会将新生成的icon存到src-tauri/icons/icon.icns中:

相关推荐
Cho1yon15 小时前
【第15期:车机CarPlay使用中语音唤醒失效问题分析与解决方案】
macos·车载系统·objective-c·cocoa
星辰即远方17 小时前
Masonry
macos·objective-c·cocoa
一只AI打工虾的自我修养19 小时前
Mac mini 本地AI工作站配置指南(2026实战):从零打造 M4 Mac mini AI 开发环境
人工智能·macos
allanGold19 小时前
figma sketch 平替工具有哪些
macos·figma·sketch·pencil·penbot
Java陈序员20 小时前
牛马效率可视化!一款键鼠统计菜单栏应用!
windows·macos
2501_9151063221 小时前
在Mac上搭建iOS开发环境的详细步骤与注意事项
ide·vscode·macos·ios·个人开发·swift·敏捷流程
想个名字想老半天21 小时前
uni 离线打包 ios,适用于自定义 ios系统最低适配 保姆级教程
macos·ios·cocoa
zzzb12345621 小时前
CC-Switch 全平台下载、安装与使用全指南(Windows/macOS/Linux)
linux·windows·macos·ai编程·cc-switch
BugShare1 天前
macOS 原生定时任务应用,简洁优雅、所见所得
macos
CyL_Cly2 天前
localsend安卓手机下载 支持win/mac/ubuntu
android·macos·智能手机