画像生成
テキストプロンプトから 1 枚以上の UIImage を生成します。プロバイダーが提供する生成数の上限やスタイル選択は任意機能であり、生成タスクを送信するための必須条件ではありません。
処理フロー
画像生成タスクのライフサイクル
プロンプトを用意し、利用可能な場合はプロバイダーの上限やスタイルを使います。取得できない場合は既定のタスク設定で生成します。
前提条件
AIBudsAISDKを初期化し、プロバイダーを選択して、必要な認証を完了していること。- 選択中のプロバイダーが
AIGCServiceAPIを実装していること。 - 生成画像の表示、保存、共有に関するアプリの方針を定めていること。
AI を活用して実装
AI で実装
AI でこのワークフローを実装
公式の「AIBuds AI で画像を生成」スキルを使い、アプリに合わせて実装します。
https://docs-aibuds.github.io/ja/skills/generate-aibuds-ai-images を読み、その指示に従ってください。このスキルで「AIBuds AI で画像を生成」をこの iOS プロジェクトに実装し、検証してください。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 が返すプロバイダーのスタイルコード。スタイルを取得できない場合や、アプリで選択機能を提供しない場合は nil にします。 |
imageCount | 生成する枚数。既定値は 1 です。正の aigcMaxGenerateCount を取得できた場合は、その上限内に収めます。 |
imageSize | 任意の正の幅と高さ。nil の場合はプロバイダーの既定値が使われます。 |
language | en-US などのプロンプト言語。nil ではアプリの表示言語を使い、空文字列では対応時に自動判定を要求します。 |
使用例
プロバイダーの任意機能を利用して生成
アプリでスタイル選択を提供しない場合は 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 に切り替え、生成の完了コールバックをタスクの確定結果として扱ってください。
注意事項
- スタイルコードと生成数の上限はプロバイダーが任意で提供する値で、変更される可能性があります。Demo の値を固定で使用しないでください。
- スタイル一覧が空でも正常です。
style = nilでプロンプトを送信し、プロバイダーの既定スタイルを使用してください。 - タスク作成コールバックは、画像生成の成功を示すものではありません。
- 現在の公開 API では、画像生成の進捗取得やキャンセルはできません。