跳到主要内容

图像生成

根据文本提示词生成一个或多个 UIImage。生成数量限制和可选风格属于 AI 服务商的可选能力,不是提交生成任务的前提条件。

动态流程

图像生成任务流程

准备提示词,在 AI 服务商提供数量限制或风格时加以使用;未提供这些能力时,使用默认任务配置继续生成。

宿主 App

准备提示词

提示词不能为空,并应清楚描述要生成的图片。

AI 服务

读取可选数量限制

AI 服务商返回正数上限时按其限制数量;无法获取时默认生成 1 张。

AI 服务 → App

获取可选风格

AI 服务商提供风格时使用返回的 styleCode;否则不指定风格。

宿主 App

配置任务

设置最终数量和风格,并按需设置图片尺寸和语言。

AI 服务

创建任务

启动生成并保留 onTaskCreated 返回的任务 ID。

任务已创建
任务结果

接收图片

根据完成回调中的 success、images、任务 ID 和 error 处理最终结果。

可选能力信息不应阻塞图像生成:无法获取时生成 1 张图片,并且不指定风格。

前提条件

  • 初始化 AIBudsAISDK、选择 AI 服务商,并在需要时完成鉴权。
  • 确认所选 AI 服务商实现 AIGCServiceAPI
  • 提前确定生成图片的展示、保存和分享规则。

使用 AI 辅助实现

使用 AI 开发

让 AI 帮助实现此工作流

使用官方“使用 AIBuds AI 生成图像”技能,根据你的 App 完成实现。

请阅读并遵循 https://docs-aibuds.github.io/zh-Hans/skills/generate-aibuds-ai-images,使用该技能在当前 iOS 项目中完成“使用 AIBuds AI 生成图像”,并验证结果。
查看官方技能

API 参考

框架

AIBudsAI.xcframework

声明

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)

请参阅 generateAIPhotofetchAigcStylesAIGCTaskConfig

任务配置

属性含义
styleAIGCStyleModel.styleCode 返回的 AI 服务商风格代码。无法获取风格或 App 不提供风格选择时保持为 nil
imageCount请求生成的图片数量。无法取得正数上限时使用默认值 1;能够取得 aigcMaxGenerateCount 时不得超过该上限。
imageSize可选的正数宽度和高度;nil 使用 AI 服务商默认值。
language提示词语言,例如 en-USnil 使用 App 当前语言,空字符串表示请求自动检测(仅适用于支持该能力的 AI 服务商)。

使用示例

使用可选的服务商能力生成图片

如果 App 不提供风格选择,可以跳过 fetchAigcStyles,直接以 nil 风格调用生成方法。下面的示例会尝试获取风格;风格列表为空或请求失败时,仍会提交提示词。

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)
}

错误处理

获取风格失败或无法取得生成数量上限,不代表图像生成失败。此时使用 style = nilimageCount = 1 提交任务,并以生成完成回调作为最终结果。

注意事项

  • 风格代码和正数数量上限属于 AI 服务商提供的可选信息,可能发生变化;不要硬编码 Demo 中的值。
  • 风格列表为空是有效情况。使用 style = nil 提交提示词,即可采用 AI 服务商的默认风格。
  • 任务创建回调只表示任务已经创建,不表示图片已经生成成功。
  • 公开 API 当前不提供图片生成进度或取消能力。