天气应用程序的图像视图中不显示天气图标

weather icon not displaying in imageview in weather pp

我正在使用 openweathermap 开发天气应用程序 api 我已经获取并设置了所有数据,还获取了字符串中的图像名称,但天气图标未显示在图像视图中。

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

这是我的代码

您可以像下面发布的那样使用原始解决方案

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
            }
        }
    }
}

最后您可以将其用作

self.vc?.weatherImage.downloadImage(for: "YOUR_URL_HERE")

同样,解决方案非常原始,除了简单地下载图像外不提供缓存功能或任何其他功能