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)!
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