BLACK FRIDAY: Save 50% on all my books and bundles! >>

How to generate images using Image Playground

Paul Hudson    @twostraws   

Updated for Xcode 16.4

When you import ImagePlayground into your project, SwiftUI gains a new imagePlaygroundSheet() modifier that allows us to use generative AI to create images based on user input.

Important: Image Playgrounds require a physical device, such as an iPhone or an iPad.

Using it means adding properties to track an image description string from the user, store whether we're currently showing the image playground UI or not, or to store the URL where the resulting image is.

As an example, this code prompts the user to describe an image, shows an image playground on demand, then shows the resulting image below:

struct ContentView: View {
    @State private var imageDescription = ""
    @State private var isShowingImagePlayground = false
    @State private var imageURL: URL?

    var body: some View {
        VStack(spacing: 20) {
            TextField("Describe an image", text: $imageDescription)
                .textFieldStyle(.roundedBorder)

            Button("Image Playground", systemImage: "apple.image.playground") {
                isShowingImagePlayground = true
            }

            AsyncImage(url: imageURL) { image in
                image
                    .resizable()
                    .scaledToFit()
            } placeholder: {
                Text("(No image)")
            }
        }
        .padding()
        .imagePlaygroundSheet(isPresented: $isShowingImagePlayground, concept: imageDescription) { url in
            if let imageURL {
                try? FileManager.default.removeItem(at: imageURL)
            }

            let newImageURL = URL.documentsDirectory.appending(path: "\(UUID()).png")
            try? FileManager.default.moveItem(at: url, to: newImageURL)
            imageURL = newImageURL
        }
    }
}

Download this as an Xcode project

Tip: Assigning new image names each time ensures the UI updates correctly.

imagePlaygroundSheet() hands us a URL on successful completion, which will point to a temporary location where the file is. It's important that you move that file somewhere else for long-term storage.

In iOS 18.4 and later there are two modifiers that adjust the way image playgrounds function:

  • The imagePlaygroundPersonalizationPolicy() modifier lets you stop users from personalizing their images using privacy-sensitive information – they can't bring in a photo of themselves, for example.
  • The imagePlaygroundGenerationStyle() modifier lets you adjust the way the image is drawn. For example, you might prefer a more realistic sketch style.

Both of these must be applied after the imagePlaygroundSheet() modifier, otherwise they'll silently do nothing.

Save 50% in my Black Friday sale.

SAVE 50% All our books and bundles are half price for Black Friday, so you can take your Swift knowledge further for less! Get my all-new book Everything but the Code to make more money with apps, get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn Swift Testing, design patterns, and more.

Save 50% on all our books and bundles!

Similar solutions…

BUY OUR BOOKS
Buy Everything but the Code Buy Pro Swift Buy Pro SwiftUI Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Interview Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let us know!

 
Unknown user

You are not logged in

Log in or create account