Initializer

init(url:scale:content:placeholder:)

Loads and displays a modifiable image from the specified URL using a custom placeholder until the image loads.

Declaration 宣言

init<I, P>(url: URL?, scale: CGFloat = 1, content: @escaping (Image) -> I, placeholder: @escaping () -> P) where Content == _ConditionalContent<I, P>, I : View, P : View

Parameters パラメータ

url

The URL of the image to display.

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 of 2 for an image that you would name with the @2x suffix if stored in a file on disk.

content

A closure that takes the loaded image as an input, and returns the view to show. You can return the image directly, or modify it as needed before returning it.

placeholder

A closure that returns the view to show until the load operation completes successfully.

Discussion 議論

Until the image loads, SwiftUI displays the placeholder view that you specify. When the load operation completes successfully, SwiftUI updates the view to show content that you specify, which you create using the loaded image. For example, you can show a green placeholder, followed by a tiled version of the loaded image:


AsyncImage(url: URL(string: "https://example.com/icon.png")) { image in
    image.resizable(resizingMode: .tile)
} placeholder: {
    Color.green
}

If the load operation fails, SwiftUI continues to display the placeholder. To be able to display a different view on a load error, use the init(url:scale:transaction:content:) initializer instead.

See Also 参照

Loading an Image