此软件包目前为未压缩的 RGB 和 RGBA PNG 文件提供了一个非常精简的 PNG 编码器。
它不依赖于任何框架,应该可以在所有 Swift 支持的平台上工作。
部分工作基于以下实现:https://github.com/jamesbowman/pngout/blob/master/pngout.c
let width = UInt32(512)
let height = UInt32(512)
var imageData: [UInt32] = .init(repeating: 0, count: Int(width * height))
var index = 0
for y: UInt32 in 0 ..< height {
for x: UInt32 in 0 ..< width {
let r: UInt32 = y & 0xFF
let g: UInt32 = x & 0xFF
let b: UInt32 = (0xFF - x & 0xFF)
imageData[index] = (r << 16) + (g << 8) + (b << 0)
index += 1
}
}
let encoder = MicroPNG()
let pngData = try! encoder.encodeRGBUncompressed(data: imageData,
width: width,
height: height)
pngData.withUnsafeBytes { pointer in
let data = Data(bytes: pointer.baseAddress!, count: pointer.count)
try! data.write(to: URL(fileURLWithPath: "EXPORT PATH"))
}