Chapter 36
Using AsyncImage in SwiftUI for Loading Images Asynchronously

In WWDC 2021, Apple announced tons of new features for the SwiftUI framework to make developers’ lives easier. AsyncImage was definitely one of the most anticipated features introduced in iOS 15. If your app needs to retrieve and display images from remote servers, this view saves you from writing your own code to handle asynchronous download.

AsyncImage is a built-in view for loading and displaying remote images asynchronously. All you need is to tell it what the image URL is, and AsyncImage will then do the heavy lifting to grab the remote image and show it on the screen.

In this chapter, I will show you how to work with AsyncImage in SwiftUI projects.

The Basic Usage of AsyncImage

The simplest way to use AsyncImage is by specifying the image URL like this:

AsyncImage(url: URL(string: imageURL))

AsyncImage then connects to the given URL and downloads the remote image asynchronously. It also automatically renders a placeholder in gray while the image is not yet ready for display. Once the image is completely downloaded, AsyncImage displays the image in its intrinsic size.

Figure 1. Using AsyncImage
Figure 1. Using AsyncImage

To access the full content and the complete source code, please get your copy at https://www.appcoda.com/swiftui.

results matching ""

    No results matching ""