0
import UIKit

class DetailsViewController: UIViewController {
   @IBOutlet weak var alpha3CodeLbl: UILabel!
   @IBOutlet weak var regionLbl: UILabel!
   @IBOutlet weak var flagImage: UIImageView!

    var countrie:jsonStruct?


    override func viewDidLoad() {
        super.viewDidLoad()
        alpha3CodeLbl.text = countrie?.alpha3Code
        regionLbl.text = countrie?.region
        let urlString = "http://restcountries.eu/rest/v2/all" + (countrie?.flag)!

        flagImage.downloadedFrom(url: url!)


    }
}

extension UIImageView {
    func downloadedFrom(url: URL, contentMode mode: UIViewContentMode = .scaleAspectFit) {
        contentMode = mode
        URLSession.shared.dataTask(with: url) { data, response, error in
            guard
                let httpURLResponse = response as? HTTPURLResponse, httpURLResponse.statusCode == 200,
                let mimeType = response?.mimeType, mimeType.hasPrefix("image"),
                let data = data, error == nil,
                let image = UIImage(data: data)
                else { return }
            DispatchQueue.main.async() {
                self.image = image
            }
            }.resume()
    }
    func downloadedFrom(link: String, contentMode mode: UIViewContentMode = .scaleAspectFit) {
        guard let url = URL(string: link) else { return }
        downloadedFrom(url: url, contentMode: mode)
    }
}

I'm trying to get the flag image form this API http://restcountries.eu/rest/v2/all

There is no error but the image does not appear. But there is no result if there anyone here can fix this issue. Please, not that everything is okay and try to get the image in too many ways but still no result.

Also, I try this

    let urlString = "http[enter image description
 here][1]://restcountries.eu" + (countrie?.flag)!
Maihan Nijat
  • 9,054
  • 11
  • 62
  • 110
Mr_A
  • 3
  • 2

1 Answers1

0

A few things to fix. You should test the url before using it.

let str = "yourURL"
guard let theUrl = URL(string: str) else { return }

As mentioned by @rmaddy in a comment above, UIImage isn't going to work with SVG files. Use a UIWebView instead

var request = URLRequest(url:theUrl)
webView.loadRequest(request)

Or try SVGKit to convert SVG as described here

ericl
  • 321
  • 1
  • 7
  • my code are working will, when change the api it's working and there is no problem my problem how to make the image appear in my imageView from this api while the image from this api https://api.opendota.com/api/heroStats apear on my imageView – Mr_A May 31 '18 at 20:44
  • PNG and SVG are two different formats and you must adapt your code. Converting SVG is probably what you will want to do. I updated my post with a link to instructions for converting SVG. – ericl May 31 '18 at 23:51
  • Thank you very much. You have explained to me many meanings and opened meanings that I did not know and made me pay attention to new things – Mr_A Jun 01 '18 at 13:03
  • absolutely but FYI i displayed the picture in webkit view without using pods or any and i know that by you telling me there is SVG image thank you – Mr_A Jun 09 '18 at 23:10