Image From URL — SwiftUI

Downloading Image from URL is easy in swift and also third party SDKs available for doing this. Also for SwiftUI, it is easy to do this. Here i will explain various ways to get image from URL.

Before iOS 15

No async library provided for SwiftUI before the release of iOS 15 for downloading image from URL. Here i will give an extension, which is least appreciable to use this for more data if come from API.

extension Image {func dataFromURL(url:URL) -> Self {if let data = try? Data(contentsOf: url) {return Image(uiImage: UIImage(data: data)!).resizable()}return self.resizable()}}

How to use this

Image(systemName: "placeholder_image")     .data(url: URL(string:<URLString>)!)

Using SDKs

But the above practice won’t work fine with professional work. So for this we can use SDKs is a best practice. Most known familiar SDK which supports SwiftUI i know is SDWebImage. Here I share a piece of code using SDWebimage SDK.

AnimatedImage(url: URL(string: <URL_String>)!).resizable().frame(width: 70, height: 70)

After iOS 15

Recent release of iOS 15, Apple provide a library to download image asynchronously from URL. Here is the piece of code to do this.

AsyncImage(url: URL(string: <URL_String>)) { image in

Happy Coding!🌎