Search Docs
Loading...
Skip to content

To Base64

Convert CE.SDK exports to Base64-encoded strings for embedding in HTML, storing in databases, or transmitting via JSON APIs.

5 mins
estimated time
GitHub

Base64 encoding transforms binary image data into ASCII text. In Swift, CE.SDK’s engine.block.export() returns a Blob (a typealias for Data), which you convert to Base64 using Foundation’s built-in base64EncodedString() method.

Export a Block to Base64#

Export a design block as a PNG and convert the resulting Data to a Base64 string.

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

The export returns a Blob (Data) containing the rendered image. Call base64EncodedString() to get the Base64 representation. This works with any MIMEType supported by the export method.

Create a Data URI#

Construct a data URI by combining the MIME type with the Base64 string. Data URIs embed image data directly in HTML or CSS without separate file references.

let mimeType: MIMEType = .png
let dataURI = "data:\(mimeType.rawValue);base64,\(blob.base64EncodedString())"

The resulting string follows the format data:image/png;base64,... and can be used anywhere a URL is expected, such as in web views or HTML templates.

Work with Different MIME Types#

CE.SDK supports multiple image formats, each with format-specific quality options through ExportOptions.

let pngBlob = try await engine.block.export(page, mimeType: .png)
let pngBase64 = pngBlob.base64EncodedString()
let jpegBlob = try await engine.block.export(
page,
mimeType: .jpeg,
options: ExportOptions(jpegQuality: 0.8),
)
let jpegBase64 = jpegBlob.base64EncodedString()
let webpBlob = try await engine.block.export(
page,
mimeType: .webp,
options: ExportOptions(webpQuality: 0.9),
)
let webpBase64 = webpBlob.base64EncodedString()
FormatOptionDefaultNotes
PNGpngCompressionLevel5Lossless, supports transparency
JPEGjpegQuality0.9Lossy, smaller file size, no transparency
WebPwebpQuality1.0Modern format, good compression

Batch Process Multiple Pages#

Export all pages in a scene to Base64 strings using the batch export API. Pass the full array of page IDs to engine.block.export(_:mimeType:), which returns an AsyncThrowingStream of blobs.

let pages = try engine.scene.getPages()
var base64Results: [String] = []
for try await pageBlob in try await engine.block.export(pages, mimeType: .png) {
base64Results.append(pageBlob.base64EncodedString())
}

The batch API reuses a single worker engine for all exports, making it more memory efficient than exporting pages individually.

When to Use Base64#

Base64 encoding is useful for:

  • Embedding images in HTML email templates or web views
  • Storing image data in text-only databases or UserDefaults
  • Transmitting images through JSON APIs that don’t support binary data
  • Creating inline data URIs for CSS backgrounds

For large images or file storage, write the Data directly to disk using Data.write(to:) instead.

Next Steps#