How can I display an image by an API URL? Swift

display image from url swift 4
uitableviewcell load image from url swift 4
ios download image from url and save in device swift
get image from url ios swift 4
show image using url swift
ios download image from url asynchronously
ios asynchronous image loading and caching swift
how to convert url to image in swift

I have an app in which it shows me data from an API. I made a service that gets the data, where URL(string = "") is the url of the API:

class Webservice {

    func getAllMatches(completion: @escaping ([Matches.Matchs]?) -> ()) {

        guard let url = URL(string: "API")
            else {
                fatalError("URL is not correct!")
        URLSession.shared.dataTask(with: url) { data, _, _ in

            let matchs = try! JSONDecoder().decode([Matches.Matchs].self, from: data!)
            DispatchQueue.main.async {


Then I made a ViewModel in which the API fields were set:

class MatchListViewModel: ObservableObject {

    @Published var matches = [MatchViewModel]()

    init() {


    func fetchMatch() {

        Webservice().getAllMatches { matches in

            if let matches = matches {
                self.matches =


class MatchViewModel {

    let id = UUID()

    var match: Matches.Matchs

    init(match: (Matches.Matchs)) {
        self.match = match

    var championship: String {

    var local_Name: String {

    var local_Image: String  {
        return self.match.local.image

    var local_goals: Double {
        return self.match.local_goals

    var local_penalty_goals: Double {
        return self.match.local_penalty_goals
    var stadium_Name: String {


In the API, there is a field that shows an image, which is contained in a URL, I leave an example:


My question is: How can I display that image via that URL? The API query works, as it brings me the information I require, but I need to show me the image through that link, which I have not achieved.

Thanks for reading!


Use a loader like this to load the data:

import Combine

public class DataLoader: ObservableObject {

    public let objectWillChange = PassthroughSubject<Data,Never>()

    public private(set) var data = Data() {
        willSet {

    private let resourseURL: URL?

    public init(resourseURL: URL?){
        self.resourseURL = resourseURL

    public func loadImage() {
        guard let url = resourseURL else {

        URLSession.shared.dataTask(with: url) { (data,_,_) in
            guard let data = data else {
            DispatchQueue.main.async {
       = data
        }   .resume()

And a struct like this to display it:

import SwiftUI 

struct WebImage: View {

    @ObservedObject private var imageLoader: DataLoader

    public init(imageURL: URL?) {
        imageLoader = DataLoader(resourseURL: imageURL)

    public var body: some View {
        if let uiImage = UIImage(data: {
            return AnyView(Image(uiImage: uiImage)
        } else {
            return AnyView(Image(systemName: "ellipsis")
                            .onAppear(perform: { self.imageLoader.loadImage() }))

How to load a remote image URL into UIImageView, Paw has a lightning fast native macOS interface to compose requests, collaborate in real-time on API specs, and generate client code for your� Let’s see this with help of an example. Create an empty project and add an empty Image view. Create its outlet and then we’ll write the code to get image from an URL. This is how our application looks at the moment. Now, let’s add code to get image from an URL.

You can initialize Data via init?(contentsOf:): and then you can use that data to initialize UIImage via init?(data:):

If you would go this way remember that this Data initializer by default doesn't run on the background thread, so you can have bad performance, probably you would like to push it to background and then on completion use GCD to use result and update UI.

Loading images from URL in Swift. Like they do in HTML?, You're writing the front-end of a fancy website. You have to load images from a remote server and you have the url. What do you do? You take� ImageWithActivityIndicator is a SwiftUI view that download and display image from URL and displaying Activity Indicator while loading.

You can use URLImage library if you do not want to write any custom classes. It has heaps of customization options you can apply to your remote image.

You can install it in your project by adding a swift package(steps).

I hope it helps.

Happy coding.

Load Image From Remote URL in Swift, To load an image from a remote URL you will need to create a URL object first. let imageUrlString = "" guard let imageUrl:URL = URL(string: imageUrlString) else { return. } Basic iOS Tutorial 2 - Creating a button and making it click through to another view - Duration: 19:48. The Bearded Programmer 32,817 views

How do I load an image by URL on iOS device using Swift?, To load an image in iOS using swift we'll make use of simple data Task session. The image needs to be loaded in background because it may� Get Image From URL Swift 3 Tutorial Creating a new Project. We will create a SingleView application again. So I just created a project named ImageLoadingExample. (You can create a project with any name). Adding UIImageView. Now from the right search UIImageView. It is a view that we can use to display images.

UIImage, Assign an image to a UIImageView object to display the image in your interface. Pass an image to other APIs that might require image data. Although image� UIImageView is designed to load only local images, but with a little work you can make it load remote images too. To get a basic solution, add an extension to UIImageView that downloads image data using a GCD background thread, then converts that to a UIImage, and loads it back into the image view on the main thread:

How to load an image from url in swift 3, In this iOS tutorial you'll learn how to load and display an image from an external url Duration: 2:48 Posted: Feb 2, 2017 Assign an image to a UIImageView object to display the image in your interface. Use an image to customize system controls such as buttons, sliders, and segmented controls. Draw an image directly into a view or other graphics context. Pass an image to other APIs that might require image data.

  • Thank , but when I try to put "WebImage" in my view, I get an error in the forEach, the error says "Type '_' has no member 'id'". :/
  • Could you provide the code where you get an error please?
  • WebImage takes a URL? into its initialiser, so you have to declare it like this: WebImage(imageURL: URL(string: ""))