이미지 생성
텍스트 프롬프트로 하나 이상의 UIImage 결과를 생성합니다. 서비스 제공자의 생성 수 제한과 선택 가능한 스타일은 부가 기능이며 생성 작업을 요청하는 데 반드시 필요하지는 않습니다.
동작 흐름
이미지 생성 작업 수명 주기
프롬프트를 준비하고 사용할 수 있으면 제공자의 생성 수 제한이나 스타일을 적용합니다. 제공되지 않으면 기본 작업 설정을 사용합니다.
사전 요구 사항
AIBudsAISDK를 초기화하고 서비스 제공자를 선택한 뒤 필요한 경우 인증을 완료합니다.- 선택한 서비스 제공자가
AIGCServiceAPI를 구현하는지 확인합니다. - 생성된 이미지를 표시, 저장, 공유하는 앱 정책을 정의합니다.
AI를 활용해 구현
AI로 구현
AI로 이 워크플로 구현
공식 “AIBuds AI로 이미지 생성” 스킬을 사용해 앱에 맞게 구현하세요.
https://docs-aibuds.github.io/ko/skills/generate-aibuds-ai-images을 읽고 지침을 따르세요. 이 스킬로 “AIBuds AI로 이미지 생성”을 이 iOS 프로젝트에 구현하고 검증하세요.API Reference
프레임워크
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는 이미지 생성 진행률이나 취소 기능을 제공하지 않습니다.