I am developing weather app with openweathermap api i have get and set all data and also getting image name in string but weather icon is not displaying in imageview.
import UIKit
import Alamofire
class WeatherViewModel{
var modelWeather2 = [MyWeatherData]()
weak var vc: ViewController?
func getWeather() {
AF.request("http://api.openweathermap.org/data/2.5/weather?q="+(vc?.cityTextField.text)!+"&units=metric&appid=88236c7916643a06e2cd64d56f4e5077", method: .get).response{
response in
// debugPrint(response)
if let data = response.data
{
do{
let apiResponse = try JSONDecoder().decode(MyWeatherData.self, from: data)
self.modelWeather2.append(apiResponse)
debugPrint(apiResponse)
DispatchQueue.main.async {
self.vc?.tempLabel.text = String(apiResponse.main.temp)
self.vc?.titleLabel.text = apiResponse.weather[0].main
self.vc?.descriptionLabel.text = apiResponse.weather[0].description
let iconUrl = "http://openweathermap.org/img/wn/\(apiResponse.weather[0].icon).png"
self.vc?.weatherImage.image = UIImage(named: iconUrl)
}
}catch let error{
print(error.localizedDescription)
}
}
}
}
}
This is my code
You can use as primitive solution as like the one posted below
extension UIImageView {
func downloadImage(for url: String) {
DispatchQueue.global(qos: .default).async {
if let url = URL(string: url), let data = try? Data(contentsOf: url), let image = UIImage(data: data) {
self.image = image
}
}
}
}
and finally you can use it as
self.vc?.weatherImage.downloadImage(for: "YOUR_URL_HERE")
Again, solution is highly primitive, doesnt provide caching capability or any other functionalities other than simply downloading the image