SwiftUI: Display Image from URL.

Ronak Patel
Aug 9, 2022

--

From iOS 15.0+, apple has introduced AsyncImage.

AsynceImage: A view that asynchronously loads and displays an image.

This view uses the shared URLSession instance to load an image from the specified URL, and then display it. For example, you can display an icon that’s stored on a server:

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

You can specify a custom placeholder using init(url:scale:content:placeholder:).

AsyncImage(url: URL(string:  "https://www.gizmochina.com/wp-content/uploads/2018/09/Apple-iPhone-Xs-Max-503x503.png")) { 
image in
image.resizable()
} placeholder: {
ProgressView()
}.frame(width: 100 ,height: 100)

For this example, SwiftUI shows a ProgressView() as a placeholder first, and then the image scaled to fit in the specified frame.

--

--