Skip to main content

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​

PropertyTypeDescription
nameNSString *Display name
styleIdentifierNSString *Stable identifier used by generation config
iconURLNSURL *Optional style icon URL

TSAIImageGenerationConfig​

PropertyTypeDescription
styleIdentifierNSString *Discovered style ID; nil leaves style unspecified
imageCountNSIntegerRequested count, limited by the provider
imageSizeCGSizeRequested pixel size
languageTSAILanguagePrompt language; Unknown uses app localization and Auto is invalid

TSAIImageGenerationResult​

PropertyTypeDescription
taskIdNSString *Client task identifier
imagesNSArray<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​

  1. Prompt must not be empty, and count must be positive and within maximumImageCount.
  2. Use a style identifier from the active provider's current snapshot.
  3. Style and generation completions run asynchronously on the main thread exactly once.
  4. Generated UIImages may consume substantial memory; persist or release them promptly.