Skip to content

Save

The CreativeEngine allows you to save scenes in a binary format to share them between editors or store them for later editing.

Saving a scene can be done as a either scene file or as an archive file. A scene file does not include any fonts or images. Only the source URIs of assets, the general layout, and element properties are stored. When loading scenes in a new environment, ensure previously used asset URIs are available. Conversely, an archive file contains within it the scene’s assets and references them as relative URIs.

Save Scenes to an Archive

In this example, we will show you how to save scenes as an archive with the CreativeEditor SDK.

As an archive, the resulting Blob includes all pages and any hidden elements and all the asset data.

To get hold of such a Blob, you need to use engine.scene.saveToArchive(). This is an asynchronous method. After waiting for the coroutine to finish, we receive a Blob holding the entire scene currently loaded in the editor including its assets’ data.

let sceneAsString = try await engine.scene.saveToString()

That Blob can then be treated as a form file parameter and sent to a remote location.

var request = URLRequest(url: .init(string: "https://example.com/upload/")!)
request.httpMethod = "POST"
let (data, response) = try await URLSession.shared.upload(for: request, from: blob)

Full Code

Here’s the full code:

import Foundation
import IMGLYEngine
@MainActor
func saveSceneToArchive(engine: Engine) async throws {
let sceneUrl =
URL(string: "https://cdn.img.ly/assets/demo/v1/ly.img.template/templates/cesdk_postcard_1.scene")!
try await engine.scene.load(from: sceneUrl)
let blob = try await engine.scene.saveToArchive()
var request = URLRequest(url: .init(string: "https://example.com/upload/")!)
request.httpMethod = "POST"
let (data, response) = try await URLSession.shared.upload(for: request, from: blob)
}

Save Scenes to a Blob

In this example, we will show you how to save scenes as a Blob with the CreativeEditor SDK.

This is done by converting the contents of a scene to a string, which can then be stored or transferred. For sending these to a remote location, we wrap them in a Blob and treat it as a file object.

To get hold of the scene contents as string, you need to use engine.scene.saveToString(). This is an asynchronous method. After waiting for the coroutine to finish, we receive a plain string holding the entire scene currently loaded in the editor. This includes all pages and any hidden elements but none of the actual asset data.

let sceneAsString = try await engine.scene.saveToString()

The returned string consists solely of ASCII characters and can safely be used further or written to a database.

let blob = savedSceneString.data(using: .utf8)!

That object can then be treated as a form file parameter and sent to a remote location.

var request = URLRequest(url: .init(string: "https://example.com/upload/")!)
request.httpMethod = "POST"
let (data, response) = try await URLSession.shared.upload(for: request, from: blob)

Full Code

Here’s the full code:

import Foundation
import IMGLYEngine
@MainActor
func saveSceneToBlob(engine: Engine) async throws {
let sceneUrl =
URL(string: "https://cdn.img.ly/assets/demo/v1/ly.img.template/templates/cesdk_postcard_1.scene")!
try await engine.scene.load(from: sceneUrl)
let savedSceneString = try await engine.scene.saveToString()
let blob = savedSceneString.data(using: .utf8)!
var request = URLRequest(url: .init(string: "https://example.com/upload/")!)
request.httpMethod = "POST"
let (data, response) = try await URLSession.shared.upload(for: request, from: blob)
}

Save Scenes to a String

In this example, we will show you how to save scenes as a string with the CreativeEditor SDK.

This is done by converting the contents of a scene to a single string, which can then be stored or transferred.

To get hold of such a string, you need to use engine.scene.saveToString(). This is an asynchronous method. After waiting for the coroutine to finish, we receive a plain string holding the entire scene currently loaded in the editor. This includes all pages and any hidden elements, but none of the actual asset data.

let sceneAsString = try await engine.scene.saveToString()

The returned string consists solely of ASCII characters and can safely be used further or written to a database.

print(sceneAsString)

Full Code

Here’s the full code:

import Foundation
import IMGLYEngine
@MainActor
func saveSceneToString(engine: Engine) async throws {
let sceneUrl =
URL(string: "https://cdn.img.ly/assets/demo/v1/ly.img.template/templates/cesdk_postcard_1.scene")!
try await engine.scene.load(from: sceneUrl)
let sceneAsString = try await engine.scene.saveToString()
print(sceneAsString)
}