Skip to content

Dean151/swift-image-generation

Repository files navigation

swift-image-generation

Image generation for Swift with a unified API across providers. Lightweight, dependency-free, back-deploys to iOS 18.

Apple's FoundationModels owns text generation in the Swift world, but it has no image-generation surface. swift-image-generation fills that gap: a standalone, pure-HTTP image-generation API with no FoundationModels dependency, the same request and result types across every provider, and a platform floor of iOS 18 / macOS 15 / tvOS 18 / watchOS 11.

The only dependency is swift-http-types.

Providers

Provider Models Backend
OpenAI gpt-image-1, DALL·E POST /images/generations
Gemini gemini-*-image, Imagen :generateContent
Apple Intelligence Image Playground On-device (iOS/macOS/visionOS 26+)

The two HTTP providers (OpenAI, Gemini) work on every platform and back-deploy to the iOS 18 floor. Apple Intelligence is an optional, on-device extra: it compiles in only where the ImagePlayground framework exists and runs only on iOS 26 / macOS 26 / visionOS 26 and later — no API key or network required.

Installation

Add the package to your Package.swift:

.package(url: "https://github.com/Dean151/swift-image-generation", from: "0.1.0"),

and depend on the ImageGeneration product:

.product(name: "ImageGeneration", package: "swift-image-generation"),

Usage

import ImageGeneration

let client = OpenAI(
    apiKey: ProcessInfo.processInfo.environment["OPENAI_API_KEY"] ?? "",
    transport: URLSessionTransport()
)

let images = try await client.imageGenerationModel(.gptImage1).generate(
    ImageGenerationRequest(prompt: "a lighthouse at dusk", count: 2),
    options: .openAI(size: .square1024, quality: .high)
)

for image in images.images {
    // image.data holds the raw bytes (e.g. PNG)
}

Gemini follows the same shape:

let gemini = Gemini(apiKey: key, transport: URLSessionTransport())
let images = try await gemini.imageGenerationModel(.gemini25FlashImage).generate(
    "a lighthouse at dusk",
    options: .gemini(aspectRatio: .landscape16x9, imageSize: .twoK)
)

On-device with Apple Intelligence

Where Image Playground is available, generate entirely on-device — no key, no network:

if #available(iOS 26, macOS 26, visionOS 26, *) {
    let apple = AppleIntelligence()
    let images = try await apple.imageGenerationModel().generate(
        "a watercolor lighthouse",
        options: .appleIntelligence(style: .illustration)
    )
}

AppleIntelligence and its options only exist where canImport(ImagePlayground) holds, so guard cross-platform call sites with #if canImport(ImagePlayground).

Testing

MockImageGenerationModel ships as public API so your own tests can drive a deterministic image-generation model without touching the network:

import ImageGeneration

let model = MockImageGenerationModel(imageData: pngBytes, format: .png)
let images = try await model.generate("anything")

License

MIT — see LICENSE.

About

Image generation for Swift with a unified API across providers. Lightweight, back-deploys to iOS 18.

Topics

Resources

License

Stars

0 stars

Watchers

0 watching

Forks

Sponsor this project

 

Contributors

Languages