图像生成
根据文本提示词生成一个或多个 UIImage。生成数量限制和可选风格属于 AI 服务商的可选能力,不是提交生成任务的前提条件。
动态流程
图像生成任务流程
准备提示词,在 AI 服务商提供数量限制或风格时加以使用;未提供这些能力时,使用默认任务配置继续生成。
前提条件
- 初始化
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
- Objective-C
/// 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)/// Styles cached by the selected image-generation provider.
@property(nonatomic, class, readonly, copy) NSArray<AIBudsAIGCStyleModel *> *_Nullable aigcStyles;
/// Maximum image count accepted by one provider task.
@property(nonatomic, class, readonly) NSInteger aigcMaxGenerateCount;
/// Fetches styles supported by the selected provider.
+ (void)fetchAigcStylesWithCompletion:(void (^ _Nullable)(NSArray<AIBudsAIGCStyleModel *> * _Nullable, NSError * _Nullable))completion;
/// Generates images from a text prompt and configuration.
+ (void)generateAIPhotoWithPrompt:(NSString * _Nonnull)prompt
config:(AIBudsAIGCTaskConfig * _Nonnull)config
taskCreated:(void (^ _Nullable)(NSString * _Nonnull))onTaskCreated
completion:(void (^ _Nullable)(NSString * _Nullable, BOOL, NSArray<UIImage *> * _Nullable, NSError * _Nullable))completion;请参阅 generateAIPhoto、fetchAigcStyles 和 AIGCTaskConfig。
任务配置
| 属性 | 含义 |
|---|---|
style | AIGCStyleModel.styleCode 返回的 AI 服务商风格代码。无法获取风格或 App 不提供风格选择时保持为 nil。 |
imageCount | 请求生成的图片数量。无法取得正数上限时使用默认值 1;能够取得 aigcMaxGenerateCount 时不得超过该上限。 |
imageSize | 可选的正数宽度和高度;nil 使用 AI 服务商默认值。 |
language | 提示词语言,例如 en-US;nil 使用 App 当前语言,空字符串表示请求自动检测(仅适用于支持该能力的 AI 服务商)。 |
使用示例
使用可选的服务商能力生成图片
如果 App 不提供风格选择,可以跳过 fetchAigcStyles,直接以 nil 风格调用生成方法。下面的示例会尝试获取风格;风格列表为空或请求失败时,仍会提交提示词。
- Swift
- Objective-C
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)
}NSString *prompt = @"A lightweight wearable assistant on a clean studio background";
void (^generate)(NSString *_Nullable) = ^(NSString *styleCode) {
NSInteger maximum = AIBudsAISDK.aigcMaxGenerateCount;
AIBudsAIGCTaskConfig *config = [[AIBudsAIGCTaskConfig alloc] init];
config.style = styleCode;
config.imageCount = maximum > 0 ? MIN(2, maximum) : 1;
config.language = @"en-US";
[AIBudsAISDK generateAIPhotoWithPrompt:prompt
config:config
taskCreated:^(NSString *taskID) {
dispatch_async(dispatch_get_main_queue(), ^{
[self showTaskID:taskID];
});
}
completion:^(
NSString *taskID, BOOL success, NSArray<UIImage *> *images, NSError *generationError) {
dispatch_async(dispatch_get_main_queue(), ^{
if (!success || images.count == 0) {
[self showError:generationError];
return;
}
[self showImages:images taskID:taskID];
});
}];
};
[AIBudsAISDK
fetchAigcStylesWithCompletion:^(NSArray<AIBudsAIGCStyleModel *> *styles, NSError *error) {
// Style discovery is optional. A nil style uses the provider default.
NSString *styleCode = error == nil ? styles.firstObject.styleCode : nil;
generate(styleCode);
}];错误处理
获取风格失败或无法取得生成数量上限,不代表图像生成失败。此时使用 style = nil 和 imageCount = 1 提交任务,并以生成完成回调作为最终结果。
注意事项
- 风格代码和正数数量上限属于 AI 服务商提供的可选信息,可能发生变化;不要硬编码 Demo 中的值。
- 风格列表为空是有效情况。使用
style = nil提交提示词,即可采用 AI 服务商的默认风格。 - 任务创建回调只表示任务已经创建,不表示图片已经生成成功。
- 公开 API 当前不提供图片生成进度或取消能力。