The URL of the image to display.
init(url:scale:transaction:content:)
Availability 有効性
- iOS 15.0+
- iPadOS 15.0+
- macOS 12.0+
- Mac Catalyst 15.0+
- tvOS 15.0+
- watchOS 8.0+
Technology
- Swift
UI
Declaration 宣言
init(url: URL
?, scale: CGFloat
= 1, transaction: Transaction
= Transaction(), content: @escaping (AsyncImagePhase
) -> Content)
Parameters パラメータ
url
scale
The scale to use for the image. The default is
1
. Set a different value when loading images designed for higher resolution displays. For example, set a value of2
for an image that you would name with the@2x
suffix if stored in a file on disk.transaction
The transaction to use when the phase changes.
content
A closure that takes the load phase as an input, and returns the view to display for the specified phase.
Discussion 議論
If you set the asynchronous image’s URL to nil
, or after you set the URL to a value but before the load operation completes, the phase is Async
. After the operation completes, the phase becomes either Async
or Async
. In the first case, the phase’s error
value indicates the reason for failure. In the second case, the phase’s image
property contains the loaded image. Use the phase to drive the output of the content
closure, which defines the view’s appearance:
AsyncImage(url: URL(string: "https://example.com/icon.png")) { phase in
if let image = phase.image {
image // Displays the loaded image.
} else if phase.error != nil {
Color.red // Indicates an error.
} else {
Color.blue // Acts as a placeholder.
}
}
To add transitions when you change the URL, apply an identifier to the Async
.