Instance Method

compress(path:level:hint:)

Encodes and compresses a PNG to a file at the given file path.

Declaration

func compress(path:String, level:Int, hint:Int) throws -> Void?

Parameters

path

A path to save the PNG file at.

level

The compression level to use. It should be in the range 0 ... 13, where 13 is the most aggressive setting. The default value is 9.

Setting this parameter to a value less than 0 is the same as setting it to 0. Likewise, setting it to a value greater than 13 is the same as setting it to 13.

hint

A size hint for the emitted IDAT chunks. It should be in the range 1 ... 2147483647. Reasonable settings range from around 1 K to 64 K. The default value is 32768 (215).

Setting this parameter to a value less than 1 is the same as setting it to 1. Likewise, setting it to a value greater than 2147483647 (231 – 1) is the same as setting it to 2147483647.

Return value

A Void tuple if the destination file could be opened successfully, or nil otherwise.

Overview

Compression level 9 is roughly equivalent to libpng’s maximum compression setting in terms of compression ratio and encoding speed. The higher levels (10 through 13) are very computationally expensive, so they should only be used when optimizing for file size.

Experimental comparisons between Swift PNG and libpng’s compression settings can be found on this page.

This interface is only available on MacOS and Linux. The compress(stream:level:hint:) function provides a platform-independent encoding interface.

Topics

See also

static func decompress<Source>(stream:inout Source) throws -> Self

Decompresses and decodes a PNG from the given bytestream.

static func decompress(path:String) throws -> Self?

Decompresses and decodes a PNG from a file at the given file path.