Swift指令碼(二):無失真壓縮專案中的png檔案

weixin_33850890發表於2017-01-01

本文將一步一步實現一個Swift指令碼,用來自動壓縮專案中的png檔案;

一、安裝ImageOptim

ImageOptim是用來壓縮png的應用程式;
1、開啟ImageOptim,裡面有個下載按鈕,下載到本地;
2、解壓ImageOptim.tbz2下載下來的檔案,得到ImageOptim;
3、將ImageOptim放到“應用程式”中;
4、測試ImageOptim:開啟ImageOptim,將一張png圖片拖入ImageOptim中,ImageOptim將在原圖上自動壓縮;

二、安裝ImageOptim-CLI命令列工具

通過ImageOptim-CLI,ImageOptim能在命令列中被開啟,我們的指令碼也正是藉助於ImageOptim-CLI來操呼叫ImageOptim的;
安裝步驟:
1、瀏覽器開啟https://codeload.github.com/JamieMason/ImageOptim-CLI/zip/1.14.8 , 下載ImageOptim-CLI
2、解壓ImageOptim-CLI-1.14.8.zip
3、將imageoptim-cli/bin新增到$PATH中
比如,您下載的目錄如下:/Users/breeze/Downloads/ImageOptim-CLI-1.14.8/bin,則在~/bash_profile檔案中,新增如下配置:

export PATH=$PATH:~/Downloads/ImageOptim-CLI-1.14.8/bin

4、測試ImageOptim-CLI:

$ imageOptim --version
1.14.8

此時說明ImageOptim-CLI安裝成功!

三、編寫Swift指令碼

1、編寫指令碼CompressPicture.swift

#!/usr/bin/env xcrun swift

import Foundation

// 判斷是否為資料夾
func isDirectory(atPath: String) -> Bool {
    var isDirectory: ObjCBool = ObjCBool(false)
    FileManager.default.fileExists(atPath: atPath, isDirectory: &isDirectory)
    
    return isDirectory.boolValue
}

// Swift3.0用CommandLine獲取使用者輸入命令
// argc是引數個數
guard CommandLine.argc == 2 else {
    print("Argument cout error: it need a file path for argument!")
    exit(0)
}

// arguments是引數
let argv = CommandLine.arguments
let filePath = argv[1]

let fileManager = FileManager.default

var isDirectory: ObjCBool = ObjCBool(false)
guard fileManager.fileExists(atPath: filePath, isDirectory: &isDirectory) else {
    print("The '\(filePath)' file path is not exit!")
    exit(0)
}

guard fileManager.isReadableFile(atPath: filePath) else {
    print("The '\(filePath)' file path is not readable!")
    exit(0)
}

let enumerator: FileManager.DirectoryEnumerator = fileManager.enumerator(atPath: filePath)!

var pngList: [String] = []
while let element = enumerator.nextObject() as? String {
    let absoluteFilePath = filePath + "/" + element
    
    guard fileManager.isReadableFile(atPath: absoluteFilePath) else {
        continue
    }
    
    if absoluteFilePath.hasSuffix(".png") {

        print("Compress \(absoluteFilePath):")
        var  shell = "echo " + absoluteFilePath + " | imageoptim"
        
        let process = Process()
        process.launchPath = "/bin/bash"
        process.arguments = ["-c", shell]
        
        process.launch()
        process.waitUntilExit()     // 等到執行完才進入下一個迴圈
    }
}

2、更改指令碼的可執行為可執行:

$ chmod +x CompressPicture.swift

3、執行指令碼

$ ./CompressPicture.swift  Your/Project/Path

執行時,在終端中將看到類似的壓縮過程:

Running ImageOptim...
TOTAL was: 10.565kb now: 5.767kb saving: 4.798kb (45.00%)
Compress /Users/breeze/dev/ShouldWinCopy//GoldenCreditease/GoldenCreditease/Images.xcassets/fund/fund_list_buy_more.imageset/icon01-2.png:
Running ImageOptim...
TOTAL was: 7.207kb now: 3.629kb saving: 3.578kb (49.00%)
Compress /Users/breeze/dev/ShouldWinCopy//GoldenCreditease/GoldenCreditease/Images.xcassets/fund/fund_list_buy_more.imageset/icon01.png:
Running ImageOptim...
TOTAL was: 7.207kb now: 3.629kb saving: 3.578kb (49.00%)
Compress /Users/breeze/dev/ShouldWinCopy//GoldenCreditease/GoldenCreditease/Images.xcassets/fund/fund_myFund_header_background.imageset/我的基金背景-1.png:

相關文章