libminipng 压缩png的swift-framework

#code

通过lodepng解析png图片,使用pngquant算法进行压缩的swift-framework,可实现PNG图片的压缩,并很好的保持图片的质量,完美实现其透明效果,支持Cocoapods安装使用

方法说明:

/// 通过PNG图片Data压缩
///
/// - Parameters:
///   - data: PNG图片Data
///   - maximum: 压缩率,0-100,100为最好
/// - Returns: 压缩后的PNG图片Data
public static func data2Data(_ data:Data,_ maximum:Int) -> Data?
/// 通过PNG图片路径压缩
///
/// - Parameters:
///   - path: PNG图片路径
///   - maximum: 压缩率,0-100,100为最好
/// - Returns: 压缩后的PNG图片Data
public static func path2Data(_ path:String,_ maximum:Int) -> Data?

Cocoapods

Podfile 添加 pod "libminipng"

运行命令 pod install

使用

引入 import libminipng

编写代码:

let path:String = "https://images.ichochy.com/Cloud.png"
let data:Data = try! Data.init(contentsOf: URL.init(string: path)!)
let maximum:Int = 100 // 压缩率,0-100,100为最好
let result:Data? = minipng.data2Data(data,maximum)
try! result?.write(to: URL.init(fileURLWithPath: "/Users/mleo/minipng.png"))
let path:String = "/Users/mleo/Cloud.png"
let maximum:Int = 100 // 压缩率,0-100,100为最好
let result:Data? = minipng.path2Data(path, maximum)
try! result?.write(to: URL.init(fileURLWithPath: "/Users/mleo/minipng.png"))

GitHub:

libminipng: https://github.com/iChochy/libminipng
pngquant: https://github.com/ImageOptim/libimagequant
lodepng: https://github.com/lvandeve/lodepng

源文:https://ichochy.com/posts/20190812/

Comments

comments powered by Disqus