### Simple Concept Text Input with SwiftUI Source: https://context7.com/matt54/imageplaygroundexamples/llms.txt Uses a text string as a concept prompt to guide the image generation process within the Image Playground sheet. ```swift import ImagePlayground import SwiftUI @available(iOS 18.1, macOS 15.1, *) struct SimpleConceptView: View { @Environment(\.supportsImagePlayground) private var supportsImagePlayground @State private var isImagePlaygroundPresented: Bool = false @State private var generatedImageURL: URL? @State private var conceptText: String = "A monkey on a pirate ship" var body: some View { VStack(spacing: 20) { VStack(alignment: .leading, spacing: 4) { Text("Concept").opacity(0.5) TextField("Enter concept", text: $conceptText) .textFieldStyle(.plain) .padding(8) .background( RoundedRectangle(cornerRadius: 4) .fill() .opacity(0.125) ) } Button(action: { isImagePlaygroundPresented = true }) { Text("Generate Image") } .disabled(!supportsImagePlayground) } .imagePlaygroundSheet( isPresented: $isImagePlaygroundPresented, concept: conceptText, // Pass the text concept here onCompletion: { url in self.generatedImageURL = url } ) } } ``` -------------------------------- ### Basic Integration with SwiftUI Source: https://context7.com/matt54/imageplaygroundexamples/llms.txt Presents the Image Playground interface as a modal sheet and handles the generated image URL upon completion. ```swift import ImagePlayground import SwiftUI @available(iOS 18.1, macOS 15.1, *) struct BasicExampleView: View { @Environment(\.supportsImagePlayground) private var supportsImagePlayground @State private var isImagePlaygroundPresented: Bool = false @State private var generatedImageURL: URL? @State private var showCancellationAlert: Bool = false var body: some View { VStack(spacing: 20) { // Display the generated image if available if let url = generatedImageURL, let source = CGImageSourceCreateWithURL(url as CFURL, nil), let cgImage = CGImageSourceCreateImageAtIndex(source, 0, nil) { Image(cgImage, scale: 1.0, label: Text("Generated Image")) .resizable() .aspectRatio(contentMode: .fit) .frame(width: 200, height: 200) .clipShape(RoundedRectangle(cornerRadius: 20)) } Button(action: { isImagePlaygroundPresented = true }) { Text(supportsImagePlayground ? "Open Image Playground" : "Not Supported") } .disabled(!supportsImagePlayground) } .imagePlaygroundSheet( isPresented: $isImagePlaygroundPresented, onCompletion: { url in self.generatedImageURL = url }, onCancellation: { showCancellationAlert = true } ) .alert("Generation Cancelled", isPresented: $showCancellationAlert) { Button("OK", role: .cancel) { } } } } ``` -------------------------------- ### Present Image Playground as Sheet in AppKit (macOS) Source: https://context7.com/matt54/imageplaygroundexamples/llms.txt Use this code to present the Image Playground as a sheet in an AppKit-based macOS application. Handle the results by conforming to the `ImagePlaygroundViewController.Delegate` protocol. Requires macOS 15.1 or later. ```swift #if os(macOS) import AppKit import ImagePlayground @available(macOS 15.1, *) class AppKitExampleViewController: NSViewController { static var isImagePlaygroundAvailable: Bool { if #available(macOS 15.1, *), ImagePlaygroundViewController.isAvailable { return true } return false } private lazy var imageView: NSImageView = { let imageView = NSImageView() imageView.translatesAutoresizingMaskIntoConstraints = false imageView.imageScaling = .scaleProportionallyUpOrDown imageView.wantsLayer = true imageView.layer?.cornerRadius = 10 imageView.layer?.backgroundColor = NSColor.systemGray.withAlphaComponent(0.2).cgColor return imageView }() private lazy var actionButton: NSButton = { let button = NSButton() button.translatesAutoresizingMaskIntoConstraints = false button.title = Self.isImagePlaygroundAvailable ? "Open Image Playground" : "Not Available" button.isEnabled = Self.isImagePlaygroundAvailable button.bezelStyle = .rounded button.target = self button.action = #selector(openPlayground) return button }() override func loadView() { self.view = NSView() view.addSubview(imageView) view.addSubview(actionButton) NSLayoutConstraint.activate([ imageView.centerXAnchor.constraint(equalTo: view.centerXAnchor), imageView.topAnchor.constraint(equalTo: view.topAnchor, constant: 20), imageView.widthAnchor.constraint(equalToConstant: 200), imageView.heightAnchor.constraint(equalToConstant: 200), actionButton.centerXAnchor.constraint(equalTo: view.centerXAnchor), actionButton.topAnchor.constraint(equalTo: imageView.bottomAnchor, constant: 20) ]) } @objc private func openPlayground() { let playground = ImagePlaygroundViewController() playground.delegate = self presentAsSheet(playground) // Present as sheet on macOS } } @available(macOS 15.1, *) extension AppKitExampleViewController: ImagePlaygroundViewController.Delegate { func imagePlaygroundViewController( _ imagePlaygroundViewController: ImagePlaygroundViewController, didCreateImageAt imageURL: URL ) { if let image = NSImage(contentsOf: imageURL) { imageView.image = image } dismiss(imagePlaygroundViewController) } func imagePlaygroundViewControllerDidCancel( _ imagePlaygroundViewController: ImagePlaygroundViewController ) { dismiss(imagePlaygroundViewController) } } #endif ``` -------------------------------- ### Integrate ImagePlaygroundViewController in UIKit Source: https://context7.com/matt54/imageplaygroundexamples/llms.txt Use this implementation to present the Image Playground interface and handle the resulting image via the delegate protocol. Requires iOS 18.1 or later. ```swift #if os(iOS) import ImagePlayground import UIKit @available(iOS 18.1, *) class UIKitExampleViewController: UIViewController { // Check if Image Playground is available on the device static var isImagePlaygroundAvailable: Bool { if #available(iOS 18.1, *), ImagePlaygroundViewController.isAvailable { return true } return false } private let imageView: UIImageView = { let imageView = UIImageView() imageView.translatesAutoresizingMaskIntoConstraints = false imageView.contentMode = .scaleAspectFill imageView.clipsToBounds = true imageView.layer.cornerRadius = 10 imageView.backgroundColor = .systemGray6 return imageView }() private lazy var actionButton: UIButton = { let button = UIButton(type: .system) button.translatesAutoresizingMaskIntoConstraints = false button.setTitle("Open Image Playground", for: .normal) button.setTitle("Not Available", for: .disabled) button.isEnabled = Self.isImagePlaygroundAvailable button.addTarget(self, action: #selector(openPlayground), for: .touchUpInside) return button }() override func viewDidLoad() { super.viewDidLoad() view.backgroundColor = .systemBackground view.addSubview(imageView) view.addSubview(actionButton) NSLayoutConstraint.activate([ imageView.centerXAnchor.constraint(equalTo: view.centerXAnchor), imageView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 20), imageView.widthAnchor.constraint(equalToConstant: 200), imageView.heightAnchor.constraint(equalToConstant: 200), actionButton.centerXAnchor.constraint(equalTo: view.centerXAnchor), actionButton.topAnchor.constraint(equalTo: imageView.bottomAnchor, constant: 20) ]) } @objc private func openPlayground() { let playground = ImagePlaygroundViewController() playground.delegate = self present(playground, animated: true) } } @available(iOS 18.1, *) extension UIKitExampleViewController: ImagePlaygroundViewController.Delegate { func imagePlaygroundViewController( _ imagePlaygroundViewController: ImagePlaygroundViewController, didCreateImageAt imageURL: URL ) { // Load and display the generated image if let imageData = try? Data(contentsOf: imageURL), let image = UIImage(data: imageData) { imageView.image = image } dismiss(animated: true) } func imagePlaygroundViewControllerDidCancel( _ imagePlaygroundViewController: ImagePlaygroundViewController ) { dismiss(animated: true) } } #endif ``` -------------------------------- ### Check Image Playground Support in SwiftUI Source: https://context7.com/matt54/imageplaygroundexamples/llms.txt This SwiftUI view checks for Image Playground support using the `@Environment` property. It displays a message indicating whether the feature is available on the current device. Requires iOS 18.1 or macOS 15.1. ```swift import ImagePlayground import SwiftUI @available(iOS 18.1, macOS 15.1, *) struct SupportCheckView: View { // Environment-based check for SwiftUI @Environment(\.supportsImagePlayground) private var supportsImagePlayground var body: some View { VStack { if supportsImagePlayground { Text("Image Playground is supported!") .foregroundColor(.green) } else { Text("Image Playground is not supported on this device") .foregroundColor(.red) } } } } ``` -------------------------------- ### Use Online Source Image Source: https://context7.com/matt54/imageplaygroundexamples/llms.txt Influences image generation by providing a source image loaded from a URL. ```swift import ImagePlayground import SwiftUI @available(iOS 18.1, macOS 15.1, *) struct OnlineSourceImageView: View { @Environment(\.supportsImagePlayground) private var supportsImagePlayground @State private var isImagePlaygroundPresented: Bool = false @State private var generatedImageURL: URL? @State private var sourceImage: Image? var body: some View { VStack(spacing: 20) { VStack(alignment: .leading, spacing: 4) { Text("Source Image").opacity(0.5) Group { if let image = sourceImage { image .resizable() .aspectRatio(contentMode: .fit) } else { ProgressView() .progressViewStyle(CircularProgressViewStyle()) } } .frame(width: 200, height: 200) .clipShape(RoundedRectangle(cornerRadius: 10)) } Button(action: loadRandomImage) { Text("Load New Random Image") } Button(action: { isImagePlaygroundPresented = true }) { Text("Generate from Source") } .disabled(!supportsImagePlayground || sourceImage == nil) } .imagePlaygroundSheet( isPresented: $isImagePlaygroundPresented, sourceImage: sourceImage, // Pass the source image onCompletion: { url in self.generatedImageURL = url } ) .onAppear { loadRandomImage() } } private func loadRandomImage() { sourceImage = nil guard let url = URL(string: "https://picsum.photos/1000") else { return } URLSession.shared.dataTask(with: url) { data, _, _ in if let data = data, let image = Image(data: data) { DispatchQueue.main.async { self.sourceImage = image } } }.resume() } } // Helper extension to create SwiftUI Image from Data extension Image { init?(data: Data) { #if os(iOS) guard let uiImage = UIImage(data: data) else { return nil } self.init(uiImage: uiImage) #else guard let nsImage = NSImage(data: data) else { return nil } self.init(nsImage: nsImage) #endif } } ``` -------------------------------- ### Check Image Playground Availability Statically (UIKit/AppKit) Source: https://context7.com/matt54/imageplaygroundexamples/llms.txt This function provides a static check for Image Playground availability, suitable for use in UIKit or AppKit applications. It returns a boolean indicating whether the feature is supported. Requires iOS 18.1 or macOS 15.1. ```swift import ImagePlayground // Static check for UIKit/AppKit func checkImagePlaygroundAvailability() -> Bool { if #available(iOS 18.1, macOS 15.1, *) { return ImagePlaygroundViewController.isAvailable } return false } ``` -------------------------------- ### Extract Concepts from Text Source: https://context7.com/matt54/imageplaygroundexamples/llms.txt Uses ImagePlaygroundConcept.extracted to generate image concepts from long-form text passages. ```swift import ImagePlayground import SwiftUI @available(iOS 18.1, macOS 15.1, *) struct ExtractedConceptView: View { @Environment(\.supportsImagePlayground) private var supportsImagePlayground @State private var isImagePlaygroundPresented: Bool = false @State private var generatedImageURL: URL? @State private var conceptText: String = """ The itsy bitsy spider climbed up the waterspout. Down came the rain And washed the spider out. Out came the sun And dried up all the rain And the itsy bitsy spider climbed up the spout again. """ @State private var conceptTitle: String = "Itsy Bitsy Spider" // Create an ImagePlaygroundConcept from long-form text var concept: ImagePlaygroundConcept { ImagePlaygroundConcept.extracted(from: conceptText, title: conceptTitle) } var body: some View { VStack(spacing: 20) { VStack(alignment: .leading, spacing: 4) { Text("Title").opacity(0.5) TextField("Enter concept title", text: $conceptTitle) .padding(8) } VStack(alignment: .leading, spacing: 4) { Text("Text").opacity(0.5) TextEditor(text: $conceptText) .frame(minHeight: 100) .padding(8) } Button(action: { isImagePlaygroundPresented = true }) { Text("Generate from Story") } .disabled(!supportsImagePlayground) } .imagePlaygroundSheet( isPresented: $isImagePlaygroundPresented, concepts: [concept], // Pass array of ImagePlaygroundConcept onCompletion: { url in self.generatedImageURL = url } ) } } ``` -------------------------------- ### Select Source Image from Photo Library in SwiftUI Source: https://context7.com/matt54/imageplaygroundexamples/llms.txt Uses PhotosPicker to select an image and passes it to the imagePlaygroundSheet modifier. Requires iOS 18.1 or macOS 15.1 or later. ```swift import ImagePlayground import PhotosUI import SwiftUI @available(iOS 18.1, macOS 15.1, *) struct PhotoLibrarySourceImageView: View { @Environment(\.supportsImagePlayground) private var supportsImagePlayground @State private var isImagePlaygroundPresented: Bool = false @State private var generatedImageURL: URL? @State private var sourceImage: Image? @State private var selectedPhotoPickerItem: PhotosPickerItem? = nil var body: some View { VStack(spacing: 20) { if let sourceImage { VStack(alignment: .leading, spacing: 4) { Text("Source Image").opacity(0.5) sourceImage .resizable() .aspectRatio(contentMode: .fit) .frame(width: 200, height: 200) .clipShape(RoundedRectangle(cornerRadius: 10)) } } // PhotosPicker for selecting source images PhotosPicker( selection: $selectedPhotoPickerItem, matching: .images, photoLibrary: .shared() ) { Label("Select Source Photo", systemImage: "photo.fill") .foregroundColor(.blue) .padding() .overlay( RoundedRectangle(cornerRadius: 8) .stroke(.blue, lineWidth: 1) ) } .buttonStyle(.plain) Button(action: { isImagePlaygroundPresented = true }) { Text("Generate from Photo") } .disabled(!supportsImagePlayground || sourceImage == nil) } .imagePlaygroundSheet( isPresented: $isImagePlaygroundPresented, sourceImage: sourceImage, onCompletion: { url in self.generatedImageURL = url } ) .onChange(of: selectedPhotoPickerItem) { _, newItem in Task { do { if let data = try await newItem?.loadTransferable(type: Data.self), let image = Image(data: data) { await MainActor.run { self.sourceImage = image } } } catch { print("Error processing image: \(error)") } } } } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.