Image Generation (TSAIImageGenerationInterface)
The provider-neutral image API supports style discovery, provider limits, multi-image generation, and logical cancellation.
Prerequisites
TSAIContext *context = [TSAIKit sharedInstance].activeContext;
if (![context supportsAIFeatures:TSAIFeatureImageGeneration]) {
return;
}
id<TSAIImageGenerationInterface> imageGeneration = context.imageGeneration;
Data models
TSAIImageGenerationStyle
| Property | Type | Description |
|---|---|---|
name | NSString * | Display name |
styleIdentifier | NSString * | Stable identifier used by generation config |
iconURL | NSURL * | Optional style icon URL |
TSAIImageGenerationConfig
| Property | Type | Description |
|---|---|---|
styleIdentifier | NSString * | Discovered style ID; nil leaves style unspecified |
imageCount | NSInteger | Requested count, limited by the provider |
imageSize | CGSize | Requested pixel size |
language | TSAILanguage | Prompt language; Unknown uses app localization and Auto is invalid |
TSAIImageGenerationResult
| Property | Type | Description |
|---|---|---|
taskId | NSString * | Client task identifier |
images | NSArray<UIImage *> * | Generated images |
Discover styles
- (nullable NSArray<TSAIImageGenerationStyle *> *)availableStyles;
- (void)fetchAvailableStylesWithCompletion:(nullable TSAIImageGenerationStylesCompletionBlock)completion;
availableStyles returns the last valid snapshot. An empty array is a valid known result; nil means that no snapshot is available.
[imageGeneration fetchAvailableStylesWithCompletion:^(NSArray<TSAIImageGenerationStyle *> *styles,
NSError *error) {
if (error) {
TSLog(@"Style lookup failed: %@", error.localizedDescription);
return;
}
for (TSAIImageGenerationStyle *style in styles) {
TSLog(@"Style: %@ (%@)", style.name, style.styleIdentifier);
}
}];
Query the count limit
- (NSInteger)maximumImageCount;
A supported provider returns a positive limit; zero means unsupported.
Generate images
- (NSString *)generateImagesWithPrompt:(NSString *)prompt
config:(TSAIImageGenerationConfig *)config
completion:(nullable TSAIImageGenerationCompletionBlock)completion;
TSAIImageGenerationStyle *style = imageGeneration.availableStyles.firstObject;
TSAIImageGenerationConfig *config =
[TSAIImageGenerationConfig configWithStyleIdentifier:style.styleIdentifier
imageSize:CGSizeMake(1024, 1024)];
config.imageCount = MIN(2, imageGeneration.maximumImageCount);
config.language = TSAILanguageEnglishUS;
NSString *taskId = [imageGeneration generateImagesWithPrompt:@"A futuristic watch in a mountain valley at dawn"
config:config
completion:^(TSAIImageGenerationResult *result, NSError *error) {
if (error) {
TSLog(@"Image generation failed: %@", error.localizedDescription);
return;
}
TSLog(@"Generated images: %lu", (unsigned long)result.images.count);
}];
Cancel generation
- (void)cancelImageGenerationWithTaskId:(NSString *)taskId;
Cancellation is logical: late provider results are dropped, but server-side work may continue.
[imageGeneration cancelImageGenerationWithTaskId:taskId];
Important notes
- Prompt must not be empty, and count must be positive and within
maximumImageCount. - Use a style identifier from the active provider's current snapshot.
- Style and generation completions run asynchronously on the main thread exactly once.
- Generated UIImages may consume substantial memory; persist or release them promptly.