In SwiftUI, how do you show a placeholder, the loaded recipe photo and an error state with AsyncImage, and where do image modifiers go?
answer
- three initialisers, rising control
- content and placeholder closures
- empty, success, failure
- modifiers inside the closure
- transaction animates the change
basics
~20 sUse AsyncImage's phase initialiser: its closure receives AsyncImagePhase, and you return a placeholder for .empty, the image for .success and an error view for .failure. Image modifiers such as resizable() go on the Image inside the closure.
solid answer
~40 s`AsyncImage` has three levels of control. `init(url:scale:)` shows a standard placeholder and then the image at its natural size. `init(url:scale:content:placeholder:)` hands you the loaded `Image` to modify and lets you supply a placeholder, but it shows that placeholder on failure too. `init(url:scale:transaction:content:)` passes an `AsyncImagePhase`: `.empty`, `.success(Image)` or `.failure(Error)`, so you can draw a spinner, the photo and a distinct error view, and the `transaction` can animate the switch. `AsyncImage` is not an `Image`, so `resizable()` and `scaledToFill()` go on the image inside the closure, and a `switch` over the phase needs `@unknown default` because the enum is not frozen. `scale` defaults to 1, so an @3x photo shown at its natural size needs `scale: 3`.
code
swift · 29 linesimport SwiftUI
struct RecipeHero: View {
let url: URL?
var body: some View {
Color.clear
.frame(height: 240)
.overlay {
AsyncImage(url: url, transaction: Transaction(animation: .easeIn)) { phase in
switch phase {
case .empty:
ProgressView()
case .success(let image):
image
.resizable()
.scaledToFill()
case .failure:
Image(systemName: "photo")
.font(.largeTitle)
.foregroundStyle(.secondary)
@unknown default:
EmptyView()
}
}
}
.clipped()
}
}go deeper
Recall the phases and that image modifiers go on the Image inside the content closure, not on AsyncImage itself.
Explain the three initialisers, why the placeholder form hides failures, the transaction parameter and the scale default of 1.
Show a stable layout across phases, @unknown default for the non-frozen enum, and a retry through a new identity.
Decide where AsyncImage is enough, such as a single hero photo, and where screens need a shared loader with caching and retries.
## What AsyncImage is `AsyncImage`, available since iOS 15, is a SwiftUI view that downloads an image from a URL and displays it. Apple's documentation says it loads through the shared `URLSession`, shows a placeholder until the load finishes and then updates to show the image. It is a convenience for simple cases such as a recipe's hero photo; it is not an image pipeline. ## Three initialisers, rising control | Initialiser | You control | What you cannot do | |---|---|---| | `init(url:scale:)` | only the URL and scale | resize the image or choose the placeholder | | `init(url:scale:content:placeholder:)` | the loaded `Image` and a placeholder view | tell loading apart from failure | | `init(url:scale:transaction:content:)` | every phase, plus the animation between them | nothing on this list | With the first form, the standard placeholder fills the available space, and the downloaded image appears at its natural size: Apple's example shows a small icon inside a larger frame. The second form is the common one. Its placeholder is shown while loading **and** after a failure, so the user cannot tell a slow network from a broken link. The documentation also warns that if the placeholder is itself an `Image` that does not load, for example a misspelt asset name, SwiftUI shows nothing and reports no error. ## The phases `AsyncImagePhase` has three cases and two convenience properties: - **`.empty`**: no image yet; the load has not finished. Show a `ProgressView` or a neutral tile. - **`.success(Image)`**: the image loaded; apply your image modifiers to it. - **`.failure(Error)`**: the load failed; show a recognisable error state, such as a `photo` symbol in a secondary colour. - **`phase.image`** and **`phase.error`** return optionals, which suit an `if let` chain like the one in Apple's example. `AsyncImagePhase` is declared in SwiftUI as a non-frozen enum, so a `switch` that lists the three cases without `@unknown default` gets a compiler diagnostic: a warning in the Swift 5 language mode and an error in Swift 6 mode. ## Where the modifiers go `AsyncImage` is a container view, not an `Image`. `resizable()`, `renderingMode(_:)` and `interpolation(_:)` are methods on `Image`, so `AsyncImage(url: url).resizable()` does not compile. Apple's documentation says to apply image-specific modifiers to the `Image` the content closure receives: 1. In the `.success` branch, call `image.resizable().scaledToFill()`. 2. Apply the frame and `clipped()` to the container around `AsyncImage`, so the placeholder and the error view occupy the same box as the photo. 3. Pass `transaction: Transaction(animation: .easeIn)` to fade the photo in when the phase changes. ## Scale, retries and limits - The **`scale`** parameter defaults to 1, so a 600-pixel-wide image lays out 600 points wide. A server that serves @3x images needs `scale: 3`, or resize the image in the closure anyway. - There is **no retry API**. A common approach is a retry button that changes a counter used with `id(_:)`, which gives the view a new identity and therefore a fresh load. - The URL is optional, so a recipe with no photo can pass `nil` and rely on the non-success branches. - What `AsyncImage` caches, and how that changed in iOS 27, is a separate topic; the phase API above is the same on every release since iOS 15. ## Common mistakes - Calling `resizable()` on the `AsyncImage` instead of on the `Image` inside the closure, which does not compile. - Framing only the loaded image, so the small spinner and the large photo occupy different sizes and the layout jumps when the photo arrives. - Using the content-and-placeholder initialiser where failures matter, so a broken URL looks like a load that never ends. - Using an asset `Image` as the placeholder and misspelling its name, which shows nothing and reports nothing. - Forgetting `scale` for high-resolution images that are not resized, so they lay out three times too large. ## A worked example The snippet below draws a recipe's hero photo in a 240-point band. A clear view fixes the band's size, the `AsyncImage` sits in an overlay so the filled photo cannot resize the band, and each phase has its own view. The spinner, the photo and the error symbol therefore occupy the same space, and the layout does not jump when the photo arrives.
- Why does init(url:scale:content:placeholder:) make error handling harder than the phase initialiser?Its placeholder closure runs for every state other than success, so a failed load keeps showing the spinner or tile you gave it, which looks like a load that never ends. The phase initialiser separates `.empty` from `.failure`, so you can show an error symbol, log the error and offer a retry.
- How do you let the user retry a failed AsyncImage load?AsyncImage has no retry method. Keep a counter in `@State`, show a Retry button in the `.failure` branch that increments it, and apply `.id(counter)` to the `AsyncImage`. The new identity makes SwiftUI create a fresh view, which starts again from `.empty` and loads the URL again.
- resizable() can be applied directly to an AsyncImage.
- The content-and-placeholder initialiser shows a separate error view when the load fails.
- AsyncImagePhase has a separate case for loading in progress besides .empty.
- AsyncImage picks the screen's scale automatically, so @3x images lay out correctly.
- A switch over the three phases compiles cleanly in Swift 6 mode.