Skip to main content

Image Generation

Generate one or more UIImage results from a text prompt. Provider limits and selectable styles are optional capabilities; they are not prerequisites for submitting a generation task.

Animated workflow

Image-generation task lifecycle

Prepare a prompt, use provider limits or styles when available, and fall back to the default task configuration when they are not exposed.

Host app

Prepare Prompt

Require a nonempty description of the images to generate.

AI service

Read Optional Limit

Use a positive provider maximum when available; otherwise request one image.

AI service → app

Load Optional Styles

Use a returned styleCode when the provider exposes styles; otherwise leave style empty.

Host app

Configure Task

Set the resolved count and style, plus optional size and language.

AI service

Create Task

Start generation and retain the task ID from onTaskCreated.

task created
Authoritative result

Receive Images

Use success, images, task ID, and error from the completion callback.

Optional capability metadata must not block generation: use one image and no explicit style when it is unavailable.

Prerequisites

  • Initialize AIBudsAISDK, select a provider, and complete provider authentication when required.
  • Confirm that the selected provider implements AIGCServiceAPI.
  • Define an application policy for displaying, storing, and sharing generated images.

Implement with AI Assistance

Build with AI

Implement this workflow with AI

Use the official Generate Images with AIBuds AI skill to adapt this workflow to your app.

Read and follow https://docs-aibuds.github.io/skills/generate-aibuds-ai-images. Use it to implement Generate Images with AIBuds AI in this iOS project and verify the result.
View official skill

API Reference

Framework

AIBudsAI.xcframework

Declaration

Swift
/// The available styles cached by the selected image-generation provider.
static var aigcStyles: [AIGCStyleModel]? { get }

/// The maximum number of images accepted in one task. A positive value can be
/// used to constrain the requested image count.
static var aigcMaxGenerateCount: Int { get }

/// Fetches styles supported by the selected provider.
public static func fetchAigcStyles(completion: ((_ styles: [AIGCStyleModel]?, _ error: NSError?) -> Void)? = nil)

/// Generates images from a text prompt and configuration.
/// - Parameters:
///   - prompt: The text prompt for image generation.
///   - config: Task configuration. Defaults to `.default`.
///   - onTaskCreated: Called with the provider task identifier.
///   - completion: Returns the task identifier, success flag, generated images,
///     and failure information.
public static func generateAIPhoto(prompt: String,
                                   config: AIGCTaskConfig = .default,
                            onTaskCreated: ((_ taskId: String) -> Void)? = nil,
                               completion: ((
                                   _ taskId: String?,
                                   _ success: Bool,
                                   _ images: [UIImage]?,
                                   _ error: NSError?
                               ) -> Void)? = nil)

See generateAIPhoto, fetchAigcStyles, and AIGCTaskConfig.

Task Configuration

PropertyMeaning
styleProvider style code returned by AIGCStyleModel.styleCode. Leave it as nil when styles are unavailable or the app does not offer style selection.
imageCountRequested count. Use the default value 1 when no positive aigcMaxGenerateCount is available; otherwise keep it within the reported limit.
imageSizeOptional positive width and height; nil uses the provider default.
languagePrompt language such as en-US; nil uses app localization and an empty string requests auto-detection when supported.

Usage Examples

Generate with Optional Provider Capabilities

If the app does not offer style selection, skip fetchAigcStyles and call the generation helper with nil. The example below attempts to load styles but still submits the prompt when the style list is empty or the request fails.

Swift
let prompt = "A lightweight wearable assistant on a clean studio background"

func generate(styleCode: String?) {
    let maximum = AIBudsAISDK.aigcMaxGenerateCount
    let config = AIGCTaskConfig()
    config.style = styleCode
    config.imageCount = maximum > 0 ? min(2, maximum) : 1
    config.language = "en-US"

    AIBudsAISDK.generateAIPhoto(
        prompt: prompt,
        config: config,
        onTaskCreated: { taskID in
            DispatchQueue.main.async { showTask(id: taskID) }
        },
        completion: { taskID, success, images, error in
            DispatchQueue.main.async {
                guard success, let images, !images.isEmpty else {
                    if let error {
                        show(error)
                    } else {
                        print("The provider returned no images")
                    }
                    return
                }
                show(images: images, taskID: taskID)
            }
        }
    )
}

AIBudsAISDK.fetchAigcStyles { styles, error in
    // Style discovery is optional. A nil style uses the provider default.
    let styleCode = error == nil ? styles?.first?.styleCode : nil
    generate(styleCode: styleCode)
}

Error Handling

A style-fetch failure or an unavailable generation limit is not a generation failure. Fall back to style = nil and imageCount = 1, then treat the generation completion callback as the authoritative task result.

Notes

  • Style codes and positive maximum counts are optional provider data and can change; do not hard-code the Demo values.
  • An empty style list is valid. Submit the prompt with style = nil to use the provider default.
  • The task-created callback does not mean images have been generated successfully.
  • The public API does not currently expose image-generation progress or cancellation.