[英]weather icon not displaying in imageview in weather pp
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.我正在使用 openweathermap api 开发天气应用程序,我已经获取并设置了所有数据,还获取了字符串中的图像名称,但天气图标未显示在 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同样,解决方案非常原始,除了简单地下载图像之外,不提供缓存功能或任何其他功能
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.