6

I am using a REST API (https://restcountries.eu/) and want to download the flag image (which is an .svg) and show it as a UIImage. I tried the standard way with:

    func requestData(at url: URL, success: @escaping (_ data: Data) -> Void, failure: ((_ error: NetworkError) -> Void)? = nil) {
    let request = URLRequest(url: url)
    let task = URLSession.shared.dataTask(with: request) { (responseData, response, responseError) in
        DispatchQueue.main.async {
            if responseError != nil {
                failure?(.failedRequest)
            } else if let data = responseData {
                success(data)
            } else {
                failure?(.corruptedData)
            }
        }
    }

    task.resume()
}

and the data downloads fine, but when I try to show the image with UIImage(data: data), the image is nil. Am I missing something?

smeshko
  • 1,184
  • 13
  • 27
  • 3
    iOS lacks native support for SVG, but there are other work arounds to make it work. – CodeBender Sep 17 '18 at 21:02
  • 2
    For SVG guidance, look here: https://stackoverflow.com/questions/35691839/how-to-display-svg-image-using-swift – JonJ Sep 17 '18 at 21:03
  • 2
    `UIImage` does not support SVG. Find flags in PNG or JPG format. – rmaddy Sep 17 '18 at 21:04
  • 4
    Possible duplicate of [How to display .svg image using swift](https://stackoverflow.com/questions/35691839/how-to-display-svg-image-using-swift) – Smartcat Sep 17 '18 at 22:10

0 Answers0