AsyncImage

A view that asynchronously loads and displays an image.

ComponentiOS 15.0+macOS 12.0+tvOS 15.0+watchOS 8.0+AsyncImage in SwiftUI

Declaration

React
import { AsyncImage } from 'swiftui-react-native';
Swift
nonisolated struct AsyncImage<Content> where Content : View

Initializers

One SwiftUI initializer backs <AsyncImage>.

React
<AsyncImage
  url?: string | null
  scale?: number
/>
Swift
nonisolated init(url: URL?, scale: CGFloat = 1) where Content == Image
  • urlstring | null

    The URL of the image to display.

  • scalenumber

    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.

    Default `1`

Also takes the common view props: modifiers, style and testID.

Overview

This view uses the shared doc://com.apple.documentation/documentation/Foundation/URLSession instance to load an image from a URL that you specify, and then display it. For example, you can display an icon that's stored on a server:

AsyncImage(url: URL(string: "https://example.com/icon.png"))
    .frame(width: 200, height: 200)

Until the image loads, the view displays a standard placeholder that fills the available space. After the load completes successfully, the view updates to display the image. In the example above, the icon is smaller than the frame, and so appears smaller than the placeholder.

Image.resizable(capInsets:resizingMode:), directly to an AsyncImage. Instead, apply them to the Image instance that your content closure gets when defining the view's appearance.

You can manipulate the loaded image in the content parameter using init(url:scale:content:placeholder:). For example, you can add a modifier to make the loaded image resizable:

AsyncImage(url: URL(string: "https://example.com/icon.png")) { image in
    image.resizable()
} placeholder: {
    ProgressView()
}
.frame(width: 50, height: 50)

With this initializer, you can also specify a custom placeholder. In the code in the previous example, SwiftUI shows a ProgressView first, and then the image scaled to fit in the specified frame:

If you use an Image as a placeholder view and it doesn't load, SwiftUI doesn't show anything as a placeholder and doesn't report an error.

To gain more control over the loading process, use the init(url:scale:transaction:content:) initializer, which takes a content closure that receives an AsyncImagePhase to indicate the state of the loading operation. Return a view that's appropriate for the current phase:

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.
    }
}

In iOS 27, macOS 27, watchOS 27, tvOS 27, and visionOS 27 and later, AsyncImage caches downloaded image data following the transport protocol. The system creates the cache with a default doc://com.apple.documentation/documentation/foundation/urlsessionconfiguration. To change the cache policy, specify the change in doc://com.apple.documentation/documentation/foundation/urlrequest, and pass it to init(request:scale:transaction:content:). To customize the download process in a specific view hierarchy, use View.asyncImageURLSession(_:) to specify a doc://com.apple.documentation/documentation/foundation/urlsession. AsyncImage uses this session to perform data tasks when downloading the image data.

From Apple’s SwiftUI documentation. Code samples there are Swift.

Relationships

Conforms to

  • View

See also

More in Text and images.