Sample code for 30+ languages & platforms
Swift

Simple AI Image Generation

See more AI Examples

Create an image by providing a text description.

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

    // This example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    let ai = CkoAi()!

    ai.provider = "openai"

    // Use your provider's API key.
    ai.apiKey = "MY_API_KEY"

    // Choose a model.
    ai.model = "gpt-5"

    let askParams = CkoJsonObject()!
    askParams.updateString(jsonPath: "image.output_format", value: "jpeg")
    askParams.updateString(jsonPath: "image.size", value: "1024x1024")
    askParams.updateString(jsonPath: "image.quality", value: "low")
    ai.setAskParams(json: askParams)

    ai.inputAddText(text: "Generate a small, cute illustration of a gray tabby cat hugging a happy otter wearing an orange scarf")

    // Ask the AI for image output.
    success = ai.ask(outputType: "image")
    if success == false {
        print("\(ai.lastErrorText!)")
        return
    }

    // Get the image response data.
    let bdImageData = CkoBinData()!
    success = ai.getOutputBd(bd: bdImageData)
    if success == false {
        print("\(ai.lastErrorText!)")
        return
    }

    bdImageData.writeFile(path: "c:/aaworkarea/out.jpg")

    print("Success.")

}