Image from Remote URL Using SDWebImageSwiftUI

Let’s learn how to present an image downloaded from a remote URL using SDWebImageSwiftUI with just one line of code! SwiftUI 2.0 & Xcode 12

Sullivan De Carli
Swift Productions
2 min readJan 25, 2021

--

SwiftUI logo and iPhone displaying an image
From the author

Difficulty: Beginner | Easy | Normal | Challenging

Environment : Xcode 12, SwiftUI, Package Manager & SDWebImageSwiftUI

Create a new Xcode Project

Open Xcode > New Project > App Template > SwiftUI Interface and call it WebImage

Xcode — new project
Screenshot from Xcode

Add SDWebImageSwiftUI via Package Manager

If you are not logged in to GitHub, first follow the tutorial from Apple

Go to file > Swift Packages > Add Packages Dependencies > insert “https://github.com/SDWebImage/SDWebImageSwiftUI" > Finish

If everything run smoothly, you can see a new section created in Xcode called Swift Packages Dependencies and you will be able to import the framework SDWebImageSwiftUI.

Screenshot from Xcode — importing SDWebImageSwiftUI

Let’s code!

Go to your ContentView.swift file and import the framework at the top:

Then, declare a WebImage such as you would declare an Image in SwiftUI, copy the code below inside the body:

That’s it! SDWebImageSwiftUI make it super easy to present an image from a remote url, with 3 others lines, we gave it a frame and an aspect ratio and we are good to go!

Screenshot from Xcode with Canva showing an image
Screenshot from Xcode

Thanks for reading! Consider subscribing to enjoy unlimited access to my articles and all of Medium through my referral link.

Originally published at https://swiftproductions.substack.com.

--

--

Sullivan De Carli
Swift Productions

Consultant in iOS Dev at Deloitte. I write about Design & App development. Let’s have a chat at hello@sullivandecarli.com