GlassGallery
| Identifier | glass-gallery |
| Module | SPGlassMedia |
| Kind | content-component — images on an opaque surface; glass only for the viewer's floating buttons |
| Platforms | iOS 26, iPadOS 26, macOS 26 |
| Example | Examples/Gallery/Media/GlassGalleryExample.swift |
Minimal usage
swift
GlassGallery(items: [
GlassGalleryItem(id: "cover", description: "Album cover", image: Image("cover")),
GlassGalleryItem(id: "photo-1", description: "Harbour at dusk") { try await loadPhoto(1) },
])Variants
| Item initialiser | Use |
|---|---|
init(id:description:image:) |
A local Image (asset catalogue, already decoded). Not cached. |
init(id:description:load:) |
@Sendable () async throws -> Image, run when the cell appears. |
init(id:description:loadCGImage:) |
Same with a CGImage, for decoders that work off the main actor. |
Parameters
| Parameter | Type | Default | Meaning |
|---|---|---|---|
items |
[GlassGalleryItem] |
— | Images in display order; identifiers must be unique. |
cache |
GlassGalleryCache? |
nil |
A cache shared with other views. By default the gallery owns one. |
cacheCapacity |
Int |
64 |
Capacity of the owned cache. |
minimumCellWidth |
CGFloat |
104 |
Smallest cell width; the adaptive grid fits as many columns as possible. |
GlassBoundedCache<Key, Value> is a public actor with a capacity and least-recently-used eviction (value(for:), insert(_:for:), removeValue(for:), removeAll(), count, keysByRecency). GlassGalleryCache is GlassBoundedCache<String, Image>.
States
- Loading: a progress indicator on the theme's surface colour.
- Loaded: the image, cropped to a square in the grid and fitted in the viewer.
- Failed: a warning symbol with "Media unavailable". Cancelled loads are not failures: the cell loads again when it reappears.
- Empty (
itemsis empty): aContentUnavailableView. - Viewer: full-screen cover on iOS and iPadOS, sheet on macOS; previous and next buttons (disabled at the ends), close button, caption, pinch and double-tap zoom from 1× to 4×, pan while zoomed.
Advanced example
swift
let sharedCache = GlassGalleryCache(capacity: 200)
GlassGallery(
items: listing.photos.map { photo in
GlassGalleryItem(id: photo.id, description: photo.caption, loadCGImage: {
let data = try await store.data(for: photo) // cancellable I/O
try Task.checkCancellation()
return try decoder.thumbnail(from: data, maxPixelSize: 600)
})
},
cache: sharedCache,
minimumCellWidth: 140
)Accessibility
- Each cell is a button labelled "Image n of m, description" with the image trait.
- In the viewer the image carries the same label and supports the VoiceOver zoom action; the buttons are labelled "Previous", "Next" and "Close".
- Keyboard: left and right arrows navigate, Escape closes.
- Failure is a symbol plus text. The double-tap zoom animation becomes a short fade with Reduce Motion.
Limits
- Loading starts in the cell's
.taskand is cancelled when the cell leaves the screen; the loader must itself honour cancellation for the work to actually stop. - The grid and the viewer use the same image: there is no separate thumbnail and full-size source. Provide an image large enough for the viewer, or downscale in the loader and accept a softer zoom.
- The cache bounds the number of images, not their memory size; choose the capacity for the image size you load.
- The viewer navigates with buttons and keys, not with a horizontal swipe.
- Zoom gestures were compiled but not exercised in a running application during verification; the clamping logic is unit-tested.