Meet AsyncImage in SwiftUI

Natalia Stele
2 min readJun 10, 2021

--

Let’s take a brief look at the new feature announced at WWDC21 that allows us download and display images asynchronously on SwiftUI.

In Xcode 13 (beta) and iOS 15, Apple introduced AsyncImage a simple way to request, download and show an image just providing the image URL.

Implementation

As you can appreciate is very simple to use it. You just need to provide the URL, and if you want a definition of the frame (not mandatory).

In case you want to process and customize your images, you could apply modifiers like, resizable, aspect ratio, corner radius, etc. Also you could define a placeholder that could be any type of View, as could see in the next example:

One thing you need to be aware is “You can’t apply image-specific modifiers, like resizable(capInsets:resizingMode:), directly to an AsyncImage”, so Apple recommends “Instead, apply them to the Image instance that your content closure gets when defining the view’s appearance.”

Control over the loading process

If you need to take some actions during the loading process, this is something that you will use:

Using the content closure that receives anAsyncImagePhase to indicate the state of the loading operation and return a view that’s appropriate for the current phase.

Advantages

  • Available: iOS 15.0+ Beta, macOS 12.0+ Beta, Mac Catalyst 15.0+ Beta, tvOS 15.0+ Beta, watchOS 8.0+ Beta. We could use it around all Apple platforms.
  • No need to include third party libraries ( for example https://github.com/dmytro-anokhin/url-image ).

Disadvantages

  • You cannot specify a cache policy in this early version

What we expect next?

This a great feature that most of us were waiting along with many others, despite doesn’t have the capability to cache images, now it’s very welcome between developers since reduce the amount of code to handle asynchronous images and also reduce the dependency of third party libraries.

We hope Apple continue working soon on this great feature, providing cache support and more.

If you want to take a look of a simple example of AsyncImage usage, you could go to my GitHub and clone this project (branch: test-async-image-ios15):

--

--

Natalia Stele
Natalia Stele

Written by Natalia Stele

iOS/Android developer, dog momma, passionate about coding and tech

No responses yet