0

I'm trying to update the tableview cell with image from web api. As per the below link, I tried it but it is giving nil in image even though image exist.

Update table cell image asynchronously.

Below is my code please let me know where I'm missing it.

DispatchQueue.global(qos: .background).async { [weak self] () -> Void in
            if let url = NSURL(string: "https://myantion.eu/data/sol.svg"){
                    if let data = NSData(contentsOf: url as URL) {
                        let imageAux = UIImage(data: data as Data)

                        DispatchQueue.main.async {
                                cell.imgFlag.image = imageAux
                        }
                    }
                }
        }

The imageAux gives me nil value even though image exist

As per the below suggestions, even tried using UIWebView still no result and below is the code:

> let fileURL:URL = URL(fileURLWithPath:
> "https://myantion.eu/data/sol.svg")
>             let req = URLRequest(url: fileURL)
>             DispatchQueue.main.async {
>                                             cell.webVw.loadRequest(req)
>                                        }
>             
>            }
cybergeeeek
  • 410
  • 8
  • 22

2 Answers2

0

I tried installing SVGKit as given in the below reference link and it works for me. How to display .svg image using swift

DispatchQueue.global(qos: .background).async { [weak self] () -> Void in
            if let url = NSURL(string: "https://restcountries.eu/data/est.svg"){
                    if let data = NSData(contentsOf: url as URL) {
                        let imageAux = UIImage(data: data as Data)

                        DispatchQueue.main.async {
                    let anSVGImage: SVGKImage = SVGKImage(data: data as Data)
                                cell.imgFlag.image = nSVGImage.uiImage
                        }
                    }
                }
        }

in ur existing code just add one line of code: let anSVGImage: SVGKImage = SVGKImage(data: data as Data) and it worked at my end.

sia
  • 1,872
  • 1
  • 23
  • 54
0

I tried using WKWebView and for me the below code is working:

if let svgString =
  try ? String(contentsOf: URL(string: "https://myantion.eu/data/sol.svg") !) {
    self.wkWebView.loadHTMLString(svgString, baseURL: URL(string: "https://myantion.eu/data/sol.svg") !)
  }
Nishu_Priya
  • 1,251
  • 1
  • 10
  • 23