Swift压缩Gif图像
Swift压缩Gif图像

Swift压缩Gif图像

在macOS中最常用的是使用gifsicleImageOptim压缩Gif图像(免费、开源、压缩效果好)。

ImageOptim为第三方GUI软件,可以显示图片压缩界面,适合不使用命令行的人。

在Swift中可以使用Gifsicle压缩图片,下面是Gifsicle的命令行运行。

Gifsicle压缩

在终端中安装:

brew install gifsicle

压缩Gif:

gifsicle -O3 --colors 256 input.gif -o output.gif

参数说明:

-O3:最高优化等级(0~3)

–colors 256:限制颜色数量(越低越小,但可能失真)

–resize 400×300:可以缩小尺寸来减少大小(可选)

集成Gifsicle

本地下载gifsicle:

brew install gifsicle
which gifsicle

which命令查找到gifsicle文件后,可以使用cp命令复制到对应的项目中。

也可以先复制到桌面,再把gifsicle文件拖入Xcode项目。

cp /usr/local/bin/gifsicle ~/Desktop

在Swift中调用Gifsicle

if let gifsiclePath = Bundle.main.path(forResource: "gifsicle", ofType: nil) {
    let process = Process()
    process.executableURL = URL(fileURLWithPath: gifsiclePath)
    process.arguments = [
        "--optimize=3",
        "--output", outputURL.path,
        inputURL.path
    ]

    let pipe = Pipe()
    process.standardOutput = pipe
    process.standardError = pipe

    do {
        try process.run()
        process.waitUntilExit()
    } catch {
        print("运行 gifsicle 失败:\(error)")
    }
}

代码解析

1、–optimize=3 vs -O3

-O3 是 短选项(shorthand)。

–optimize=3 是 长选项(long form)。

两者作用完全一样:表示进行第 3 级优化(最大优化)。

优化等级的区别大致是:

-O1:基本优化(移除未使用帧)

-O2:进一步合并相似帧

-O3:最大化优化,尝试减少颜色表等

2、–output vs -o

-o output.gif 是短写法。

–output output.gif 是长写法。

作用一样:指定输出文件路径。

3、–colors

在Swift代码中没有限制颜色数,所以gifscle会保留原图的颜色(最多为256,GIF格式限制)。

如果需要限制颜色,比如64色,需要在—output前面插入:

"--colors", "64"

总结

gifscle在实际测试中,对于颜色数比较多(193)的大图来说,压缩率很低,可能只有3%。对于单色的小图,压缩率可以达到77%。

在压缩时,基本上只能用O3压缩,O1存在反向压缩的场景。对于颜色数多的图像,可以设置压缩数为64,但是还是会存在很大的色差,因此在大图压缩方面不好。

在实际使用中,推荐动态调整颜色数,默认压缩登记为O3。

如果Xcode项目为新项目,则需要添加Hardened Runtime支持。

在终端中使用codesign命令给文件签名:

codesign --force --options runtime \
  --entitlements "/path/to/entitlements.plist" \
  --sign "Developer ID Application: xxx" \
  "/path/to/可执行文件路径"

详细请见《Xcode打包报错:App sandbox not enabled. The following executables must include the “com.apple.security.app-sandbox” entitlement with a Boolean value of true in the entitlements property list》。

相关文章

1、Gifsicle:https://www.lcdf.org/gifsicle/

2、ImageOptim:https://imageoptim.com/mac

3、Swift压缩PNG图像:https://fangjunyu.com/2025/07/14/swift%e5%8e%8b%e7%bc%a9png%e5%9b%be%e5%83%8f/

4、Xcode报错:”pngquant” must be rebuilt with support for the Hardened Runtime. Enable the Hardened Runtime capability in the project editor:https://fangjunyu.com/2025/07/14/xcode%e6%8a%a5%e9%94%99%ef%bc%9apngquant-must-be-rebuilt-with-support-for-the-hardened-runtime-enable-the-hardened-runtime-capability-in-the-project-editor/

5、Xcode打包报错:App sandbox not enabled. The following executables must include the “com.apple.security.app-sandbox” entitlement with a Boolean value of true in the entitlements property list:https://fangjunyu.com/2025/07/17/xcode%e6%89%93%e5%8c%85%e6%8a%a5%e9%94%99%ef%bc%9aapp-sandbox-not-enabled-the-following-executables-must-include-the-com-apple-security-app-sandbox-entitlement-with-a-boolean-value-of-true-in-the/

   

如果您认为这篇文章给您带来了帮助,您可以在此通过支付宝或者微信打赏网站开发者。

欢迎加入我们的 微信交流群QQ交流群,交流更多精彩内容!
微信交流群二维码 QQ交流群二维码

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注