FREE TRIAL: Accelerate your app development career with Hacking with Swift+! >>

How to load a remote image from a URL

Paul Hudson    @twostraws   

Updated for Xcode 13.0

New in iOS 15

SwiftUI has a dedicated AsyncImage for downloading and displaying remote images from the internet. In its simplest form you can just pass a URL, like this:

AsyncImage(url: URL(string: "https://www.hackingwithswift.com/img/paul.png"))

Download this as an Xcode project

A phone showing an image of Paul Hudson.

Note how the URL is optional – the AsyncImage will simply show a default gray placeholder if the URL string is invalid. And if the image can’t be loaded for some reason – if the user is offline, or if the image doesn’t exist – then the system will continue showing the same placeholder image.

A phone showing a large gray placeholder rectangle.

Because SwiftUI has no idea how big the downloaded image is going to be, by default AsyncImage has a flexible width and height while it’s loading. As a result, unless you specify otherwise it will take up a lot of space in your UI while the image loads, then jump to the correct size as soon as the image is loaded.

Although you can attach a frame to your image, it will only affect the placeholder by default – if your finished image arrives at a different size, your UI will have to adapt to fit it.

A better solution is to add functions to control how the resulting image is shown and what kind of placeholder you want. For example, this fetches our image and makes it resizable, but while it’s loading uses a red placeholder color:

AsyncImage(url: URL(string: "https://www.hackingwithswift.com/img/paul-2.png")) { image in
    image.resizable()
} placeholder: {
    Color.red
}
.frame(width: 128, height: 128)
.clipShape(RoundedRectangle(cornerRadius: 25))

Download this as an Xcode project

A rounded red rectangle as a placeholder.

An image with rounded corners of Paul Hudson smiling.

Because both the resulting image and the placeholder color are now resizable, the frame() modifier is able to make sure our AsyncImage stays at the correct size the entire time. Before you ask: no, there is no resizable() modifier available directly on AsyncImage.

Note: By default the image is assumed to have a scale of 1, meaning designed for non-retina screens. However, you can also control the scale with a second parameter if you already know the correct scale:

AsyncImage(url: URL(string: "https://www.hackingwithswift.com/img/paul@2x.png"), scale: 2)

Download this as an Xcode project

An image of Paul Hudson.

Hacking with Swift is sponsored by Sentry

SPONSORED With Sentry’s error and performance monitoring for iOS you see mobile vitals that actually matter, can solve any latency issues quickly, and learn how each release is performing over time.

Get started

Sponsor Hacking with Swift and reach the world's largest Swift community!

Similar solutions…

BUY OUR BOOKS
Buy Pro Swift Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift (Vapor Edition) Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Server-Side Swift (Kitura Edition) Buy Beyond Code

Was this page useful? Let us know!

Average rating: 3.7/5

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.