Implementing asynchronous images in SwiftUI
Most of the app you are going to implement depends on a remote service that provides data.
Imagine an e-commerce app or a hotel booking app.
All the content is on the server, and the app must download it and present it smoothly, taking care of the possible delay, lag, error, and so on. An essential part of this content is images. We could download and synchronously present images, but that would mean blocking the UI waiting for the download, making the app clunky and unpleasant to use.
That said, implementing images that asynchronously download and present themselves is a valuable tool to have in our toolbox.
In this recipe, we are going to implement an AsyncImage
component that you can reuse in your apps whenever you need this feature.
Getting ready
No external resources are needed in this recipe, so just create a SwiftUI project in Xcode called AsyncImageApp
.
How to do it
Two ingredients make the recipe: an ImageFetcher...