dean151/swift-image-generation
Image generation for Swift with a unified API across providers. Lightweight, dependency-free, back-deploys to iOS 18.
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.
Package Metadata
Repository: dean151/swift-image-generation
Default branch: main
README: README.md