इमेज जनरेशन
Text prompt से एक या अधिक UIImage परिणाम बनाएँ। Provider limits और चुनने योग्य styles optional capabilities हैं; generation task भेजने के लिए आवश्यक नहीं।
Image-generation task lifecycle
Prompt तैयार करें; उपलब्ध हों तो provider limits या styles उपयोग करें, अन्यथा default task configuration अपनाएँ।
आवश्यक शर्तें
AIBudsAISDKinitialize करें, सेवा प्रदाता चुनें और आवश्यकता होने पर authentication पूरा करें।- पुष्टि करें कि चुना गया सेवा प्रदाता
AIGCServiceAPIimplement करता है। - बनी images दिखाने, सहेजने और साझा करने के लिए app policy तय करें।
AI की सहायता से लागू करें
इस वर्कफ़्लो को AI से लागू करें
आधिकारिक “AIBuds AI से चित्र बनाएँ” स्किल से वर्कफ़्लो को अपने ऐप के अनुसार लागू करें।
https://docs-aibuds.github.io/hi/skills/generate-aibuds-ai-images को पढ़ें और निर्देशों का पालन करें। इस स्किल से “AIBuds AI से चित्र बनाएँ” को इस iOS प्रोजेक्ट में लागू करें और परिणाम सत्यापित करें।API Reference
Framework
AIBudsAI.xcframework
Declaration
- 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 देखें।
Task कॉन्फ़िगरेशन
| प्रॉपर्टी | अर्थ |
|---|---|
style | AIGCStyleModel.styleCode से मिला provider style code। Styles उपलब्ध न हों या app style selection न दे तो इसे nil रखें। |
imageCount | मांगी गई संख्या। Default 1 उपयोग करें जब positive aigcMaxGenerateCount न मिले; अन्यथा बताई गई limit के भीतर रखें। |
imageSize | Optional positive width और height; nil होने पर provider default उपयोग होता है। |
language | en-US जैसी prompt language; nil app localization उपयोग करता है और empty string supported होने पर auto-detection मांगती है। |
उपयोग के उदाहरण
वैकल्पिक Provider Capabilities के साथ बनाएँ
App style selection न देता हो तो fetchAigcStyles छोड़ें और generation helper को nil के साथ call करें। नीचे का उदाहरण styles load करने की कोशिश करता है, लेकिन list खाली हो या request विफल हो तब भी prompt भेजता है।
- 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 fetch विफल होना या generation limit उपलब्ध न होना generation failure नहीं है। style = nil और imageCount = 1 उपयोग करें; generation completion callback को task का authoritative result मानें।
ध्यान देने योग्य बातें
- Style codes और positive maximum counts optional provider data हैं और बदल सकते हैं; Demo values hard-code न करें।
- Empty style list मान्य है। Provider default के लिए prompt को
style = nilके साथ भेजें। - Task-created callback का अर्थ images सफलतापूर्वक बन जाना नहीं है।
- Public API अभी image-generation progress या cancellation उपलब्ध नहीं कराती।