Search Docs
Loading...
Skip to content

To PNG

Export CE.SDK designs to PNG format with full alpha support and lossless compression—ideal for graphics, UI elements, and any content where transparency or pixel-perfect edges matter.

5 mins
estimated time
GitHub

PNG uses lossless compression and preserves transparency through an alpha channel. The encoder lets you trade encoding speed for file size without affecting image quality.

This guide covers exporting to PNG, configuring compression and dimensions, and saving exports to disk.

Export to PNG#

Export a design block by calling engine.block.export(_:mimeType:options:) with .png as the MIME type. The call returns a Blob (a Data value) containing the encoded image.

let blob: Blob = try await engine.block.export(page, mimeType: .png)

Pass a page returned by engine.scene.getPages(), or any other block ID, to export specific elements.

Export Options#

PNG export reads these fields from ExportOptions:

OptionTypeDefaultDescription
pngCompressionLevelInt5Compression level from 0 (fastest) to 9 (smallest). Quality is unaffected
targetWidthFloat0Output width in pixels. Used together with targetHeight; 0 disables the override
targetHeightFloat0Output height in pixels. Used together with targetWidth; 0 disables the override
allowTextOverhangBoolfalseWhen true, text blocks export with the full glyph bounds visible

Compression Level#

The pngCompressionLevel field (09) controls the trade-off between file size and encoding speed. Higher values produce smaller files but take longer to encode. PNG compression is lossless, so quality is never affected.

let compressedBlob = try await engine.block.export(
page,
mimeType: .png,
options: ExportOptions(pngCompressionLevel: 9),
)
  • 0 — No compression, fastest encoding
  • 5 — Balanced (default)
  • 9 — Maximum compression, slowest encoding

Target Dimensions#

Specify targetWidth and targetHeight together to export at exact dimensions. The output fills the target size while maintaining aspect ratio.

let sizedBlob = try await engine.block.export(
page,
mimeType: .png,
options: ExportOptions(targetWidth: 1920, targetHeight: 1080),
)

If the target aspect ratio differs from the block’s aspect ratio, the output extends beyond the target on one axis to preserve proportions.

Save to File System#

The returned Blob is a Data value, so writing it to disk is a single call to write(to:).

let outputURL = FileManager.default.temporaryDirectory.appendingPathComponent("export.png")
try blob.write(to: outputURL)

When to Use PNG#

PNG works well for:

  • Graphics with sharp edges, text, and UI elements
  • Designs that require transparency
  • Logos, icons, and illustrations where pixel-perfect output matters

Troubleshooting#

File too large — Increase pngCompressionLevel toward 9, or reduce dimensions with targetWidth and targetHeight. For photographic content, switch to JPEG or WebP.

Encoding feels slow — Lower pngCompressionLevel toward 0. The default 5 is balanced; 0 disables compression entirely for the fastest encode.

Transparent areas appear black — Ensure the page or block has a transparent background fill. PNG preserves alpha when the source block is transparent.

API Reference#

MethodDescription
engine.block.export(_:mimeType:options:)Export a block to the specified format
engine.scene.load(from:)Load a scene from a remote URL
engine.scene.getPages()Return all pages in the current scene
ExportOptionsFormat-specific export configuration; PNG reads pngCompressionLevel, targetWidth, targetHeight, allowTextOverhang

Next Steps#

  • Export Overview — Compare all available export formats
  • Export to JPEG — Use the photo-friendly format when transparency isn’t needed
  • Partial Export — Export specific blocks, groups, or page elements instead of entire scenes
  • Export Size Limits — Check device export limits before exporting large designs