tayloraswift / swift-png

decode, inspect, edit, and encode png images in pure swift

Home Page:https://swiftinit.org/docs/swift-png

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Creating in-memory image

croese opened this issue · comments

I'm looking to generate PNG images in memory rather than loading from a file - is there a way to construct an image of a given size with, for example, all pixels initialized to a given color? I don't see anything obvious in the api file but I wanted to see if I was missing something.

Thank you for creating this library.

(sorry for the late response, i didn’t get a notification)

this is described in one of the tutorials here (the sepia.swift one). Just conform your in-memory destination buffer type to:

protocol DataDestination
{
    mutating
    func write(_ buffer:[UInt8]) -> Void?
}

and use the compress<Destination>(to:chunkSize:level:) function rather than the compress<Destination>(path:chunkSize:level:) function.

generate solid-color pixel arrays just like any other homogenous Swift array:

let pixels:[PNG.RGBA<UInt8>] = .init(repeating: color, count: width * height)

Resolved in 4.0.0 release (see in-memory image tutorial)