SwiftUI: Display Image from URL.

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.

--

--

iOS Developer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store