版本記錄
版本號 | 時間 |
---|---|
V1.0 | 2021.11.22 星期一 |
前言
隨着App的持續功能迭代和常年的運營,最終包都會越來越大,包太大蘋果那邊會給限制,不利於用戶下載。所以App瘦身也是一項需要持續做的事。正好我這幾天就在做瘦身的事,這裏記錄一下,大家一起學習和交流。感興趣的可以看下面幾篇文章。
1. App包瘦身(一) —— App包瘦身初探(一)
2. App包瘦身(二) —— 基於pre-commit的圖片提交的自動化壓縮(一)
主要內容
前面一篇2. App包瘦身(二) —— 基於pre-commit的圖片提交的自動化壓縮(一)中,針對提交的圖片進行自動化壓縮,但是分析起來,那個方案有幾個缺點。
- 依賴
tiny.png
的三方服務的key
使用是有限制的,一般一個key
只能壓縮500
次,次數滿了就要換key。 - 無法在某一個目錄對已有圖片進行大批量的壓縮。只能針對新添加的圖片進行自動壓縮。
針對上面的缺點,這裏又有一個新的方案,就是使用imageOptim
對項目中或者目標文件夾裏的圖片進行大批量的壓縮。
這個方案其實來自於ImageOptim-CLI。
imageOptim簡介
首先我們看下要使用的程序imageOptim
。可以直接去官網下載,它可以進行有損和無損壓縮,安裝好了應用程序如下所示。
啓動頁面如下所示:
這個下載問題就不多說了,大家應該沒問題。
壓縮前的配置工作
1. Homebrew
這個就不多說了吧,沒有安裝的直接搜命令行直接安裝就行了。
2. 安裝npm
就使用下面指令進行安裝
brew install node
然後報錯了
`xxxx@bogon ~ % brew install node`
`fatal: Could not resolve HEAD to a revision`
`==> Tapping homebrew/cask`
`Cloning into ``'/usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask'``...`
`fatal: unable to access ``'[https://github.com/Homebrew/homebrew-cask/'](https://github.com/Homebrew/homebrew-cask/')``: Failed to connect to github.com port ``443``: Operation timed out`
`Error: Failure ``while` `executing; `git clone https:``//github.com/Homebrew/homebrew-cask /usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask --origin=origin --template=` exited with 128.`
這個通過下面命令解決了
cd /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core
git fetch --prune origin
git pull --rebase origin master
然後我們繼續執行brew install node
命令。
xxx@bogon ~ % brew install node
==> Downloading https://ghcr.io/v2/homebrew/core/brotli/manifests/1.0.9
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/brotli/blobs/sha256:9d3009fd246d0f6cf9fd11d0a3bd388f6c043c75fa302dec
==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sha256:9d3009fd246d0f6cf9fd11d0a3bd388f
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/c-ares/manifests/1.18.1
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/c-ares/blobs/sha256:d3dd43338a6003320bfc94466887a2336f2a8bb360913266
==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sha256:d3dd43338a6003320bfc94466887a233
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/icu4c/manifests/69.1
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/icu4c/blobs/sha256:d46b8ec5c3db629e7848e9fd31e5ec99ed952d9c81c8936a2
==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sha256:d46b8ec5c3db629e7848e9fd31e5ec99
#################################################### 73.2%
curl: (18) transfer closed with 7644301 bytes remaining to read
Error: node: Failed to download resource "icu4c"
Download failed: https://ghcr.io/v2/homebrew/core/icu4c/blobs/sha256:d46b8ec5c3db629e7848e9fd31e5ec99ed952d9c81c8936a2511fae803d831fd
xxx@bogon ~ %
就會發現又報錯了。這個時間有點長了,忘記怎麼去解決了,好像是brew update
一下就好了。
這個問題解決以後就繼續運行brew install node
。輸出如下,發現又報錯了。
xxx@bogon ~ % brew install node
==> Downloading https://ghcr.io/v2/homebrew/core/brotli/manifests/1.0.9
Already downloaded: /Users/xxx/Library/Caches/Homebrew/downloads/922ce7b351cec833f9bd2641f27d8ac011005f8b1f7e1119b8271cfb4c0d3cd7--brotli-1.0.9.bottle_manifest.json
... ... ... ...
==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sha256:4fbd4a9e3eb49c27e83bd125b0e76d386c0e12ae1139d4dc
######################################################################## 100.0%
Error: [email protected]: the bottle needs the Apple Command Line Tools to be installed.
You can install them, if desired, with:
xcode-select --install
You can try to install from source with:
brew install --build-from-source [email protected]
這個下面提示瞭解決辦法,可以直接運行命令brew install --build-from-source [email protected]
。好了以後我們接着運行命令brew install node
。就會發現仍然會報錯。
xxx@bogon ~ % brew install node
==> Downloading https://ghcr.io/v2/homebrew/core/brotli/manifests/1.0.9
Already downloaded: /Users/xxx/Library/Caches/Homebrew/downloads/922ce7b351cec833f9bd2641f27d8ac011005f8b1f7e1119b8271cfb4c0d3cd7--brotli-1.0.9.bottle_manifest.json
... ... ... ...
==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sha256:96acaa07d3255dcb75370a296ea8977848ce155f6b1496df
########################################################### 83.2%
curl: (92) HTTP/2 stream 0 was not closed cleanly: PROTOCOL_ERROR (err 1)
Error: node: Failed to download resource "node"
Download failed: https://ghcr.io/v2/homebrew/core/node/blobs/sha256:96acaa07d3255dcb75370a296ea8977848ce155f6b1496df108f06ac5c492fa2
xxx@bogon ~ %
這個問題使用http版本1.1強制git就好了,運行下面命令:
git config --global http.version HTTP/1.1
繼續brew install node
經過不懈努力,各種報錯,終於安裝成功了。
直接在控制檯輸入npm
,輸出如下所示:
xxx@bogon ~ % npm
npm <command>
Usage:
npm install install all the dependencies in your project
npm install <foo> add the <foo> dependency to your project
npm test run this project's tests
npm run <foo> run the script named <foo>
npm <command> -h quick help on <command>
npm -l display usage info for all commands
npm help <term> search for help on <term>
npm help npm more involved overview
All commands:
access, adduser, audit, bin, bugs, cache, ci, completion,
config, dedupe, deprecate, diff, dist-tag, docs, doctor,
edit, exec, explain, explore, find-dupes, fund, get, help,
hook, init, install, install-ci-test, install-test, link,
ll, login, logout, ls, org, outdated, owner, pack, ping,
pkg, prefix, profile, prune, publish, rebuild, repo,
restart, root, run-script, search, set, set-script,
shrinkwrap, star, stars, start, stop, team, test, token,
uninstall, unpublish, unstar, update, version, view, whoami
Specify configs in the ini-formatted file:
/Users/xxx/.npmrc
or on the command line via: npm <command> --key=value
More configuration info: npm help config
Configuration fields: npm help 7 config
[email protected] /usr/local/lib/node_modules/npm
這就說明npm
安裝成功了。
3. 安裝imageoptim-cli
運行下面命令行
brew install imageoptim-cli
這個很順利,直接就成功了。
輸入imageoptim --help
xxx@bogon ~ % imageoptim --help
Usage: [stdin] [options] [patterns...]
Options:
-V, --version output the version number
-a, --imagealpha enable ImageAlpha
-j, --jpegmini enable JPEGmini
-C, --no-color output to the terminal without colors
-I, --no-imageoptim disable ImageOptim
-Q, --no-quit do not quit apps once finished
-S, --no-stats do not display file size savings and quality loss information
--number-of-colors <n> ImageAlpha palette size, defaults to 256
--quality <min>-<max> ImageAlpha quality range from 0-100, defaults to 65-80
--speed <n> ImageAlpha speed from 1 (brute-force) to 10 (fastest), defaults to 1
-h, --help output usage information
Supported Apps:
ImageAlpha: https://pngmini.com
ImageOptim: https://imageoptim.com
JPEGmini Lite: https://itunes.apple.com/us/app/jpegmini-lite/id525742250
JPEGmini Pro: https://itunes.apple.com/us/app/jpegmini-pro/id887163276
JPEGmini: https://itunes.apple.com/us/app/jpegmini/id498944723
Examples:
Run ImageOptim.app over every image in current directory
imageoptim
Run ImageAlpha.app and ImageOptim.app over every PNG in current directory
imageoptim --imagealpha '**/*.png'
Run JPEGmini.app and ImageOptim.app over every JPG in current directory
imageoptim --jpegmini '**/*.jpg' '**/*.jpeg'
Run JPEGmini.app over every JPG in current directory
imageoptim --jpegmini --no-imageoptim '**/*.jpg' '**/*.jpeg'
Run ImageOptim.app over every image in a specific directory
imageoptim '~/Desktop'
xxx@bogon ~ %
到此爲止,所有的準備都基本結束了,下面我們就進行測試工作。
測試工作
1. 本地文件夾模擬測試
首先我們需要準備下測試資源。如下圖所示:
運行下程序進行壓縮
可以看見壓縮成功了,下面我們看測試資源文件夾。
可以看見,原目錄的圖片從4.2M壓縮到了787K。並且壓縮圖片替換了原有待壓縮圖片,這個就很爽了。
下面接着在測試文件夾放2張圖片,繼續使用imageOptim
進行壓縮,控制檯輸出如下:
2. 在主工程進行全量壓縮
下面就使用imageOptim
對項目文件夾進行全量的壓縮。具體輸出如下,非常長,大約耗時十分鐘左右,中間就省略了。
i Running ImageOptim...
✓ /Users/xxx/Desktop/工程目錄/Pod/BlordResources/Image/Mine/[email protected] was: 288B now: 286B saving: 2B (0.69%)
✓ /Users/xxx/Desktop/工程目錄/Pod/BlordResources/Image/Mine/[email protected] was: 375B now: 360B saving: 15B (4.00%)
✓ /Users/xxx/Desktop/工程目錄/Pod/BlordResources/Image/Mine/[email protected] was: 296B now: 280B saving: 16B (5.41%)
... ... ... ...省略好多行
✓ TOTAL was: 773kB now: 723kB saving: 50.1kB (6.48%)
i Running ImageOptim...
✓ /Users/xxx/Desktop/工程目錄/Pod/Resources/Image.xcassets/Order/bts_detail_comment_titleView.imageset/[email protected] was: 6.44kB now: 6.36kB saving: 86B (1.33%)
✓ /Users/xxx/Desktop/工程目錄/Pod/Resources/Image.xcassets/Order/bts_detail_fee_note_icon.imageset/[email protected] was: 391B now: 389B saving: 2B (0.51%)
... ... ... ...省略好多行
✓ /Users/xxx/Desktop/工程目錄/Pod/Resources/Animation.xcassets/Animation/safeCenter/2_img_1.imageset/2_img_1.png was: 1.64kB now: 1.64kB saving: 0B (0.00%)
✓ TOTAL was: 1.5MB now: 1.27MB saving: 229kB (15.29%)
i Running ImageOptim...
✓ /Users/xxx/Desktop/工程目錄/Pod/Resources/Animation.xcassets/Animation/safeCenter/2_img_2.imageset/2_img_2.png was: 3.88kB now: 3.85kB saving: 25B (0.64%)
✓ /Users/xxx/Desktop/工程目錄/Pod/Resources/Animation.xcassets/Animation/safeCenter/3_img_0.imageset/3_img_0.png was: 368B now: 356B saving: 12B (3.26%)
... ... ... ...省略好多行
✓ /Users/xxx/Desktop/工程目錄/Pod/Resources/Animation.xcassets/Animation/safeCenter/bts_sc_male_red/images/img_0.imageset/img_0.png was: 4.16kB now: 1.87kB saving: 2.29kB (55.01%)
✓ TOTAL was: 537kB now: 445kB saving: 91.2kB (16.99%)
✓ Finished
根據統計,一共壓縮了幾百張圖片,節省空間400K
左右,這就完成了對項目裏所有圖片的批量壓縮。
後記
本篇主要講述了基於
imageOptim
的大批量圖片的壓縮,感興趣的給個贊或者關注~~~~