如何从带有 JSON 数据的字符串中获取 UIImage

问题描述 投票:0回答:2

我正在尝试从API

OpenWeatherMap
加载图标图像,并将其显示在
ImageView
中。我正在尝试将其加载到
imageView
'
iconImage
' 中。我已成功加载位置和湿度的 JSON 数据,因为它们是
Strings
,但图标数据也是
String
,我无法将其显示为
UIImage

代码如下:

我的 JSON 结构如下:

struct Coordinate : Decodable {
        let lat, lon : Double?
    }
    
    struct Weather : Decodable {
        var id : Int?
        var main, myDescription, icon : String?
        
        enum CodingKeys : String, CodingKey {
            case id = "id"
            case main = "main"
            case icon = "icon"
            case myDescription = "description"
        }
    }
    
    struct Sys : Decodable {
        let type, id : Int?
    let sunrise, sunset : Date?
    let message : Double?
    let country : String?
}

struct Main : Decodable {
    let temp, tempMin, tempMax : Double?
    let pressure, humidity : Int?
}

struct Wind : Decodable {
    let speed : Double?
    let deg : Int?
}

struct MyWeather : Decodable {
    let coord : Coordinate?
    let cod, visibility, id : Int?
    let name : String?
    let base : String?
    let weather : [Weather]?
    let sys : Sys?
    let main : Main?
    let wind : Wind?
    let dt : Date?
}`enter code here`    

查看下面的控制器:

  guard let APIUrl = URL(string: "https://api.openweathermap.org/data/2.5/weather?q=" + text +  "&appid=e7b2054dc37b1f464d912c00dd309595&units=Metric") else { return }
    //API KEY
    
    URLSession.shared.dataTask(with: APIUrl) { data, response, error in
        guard let data = data else { return }
        
        
        let decoder = JSONDecoder()
        //Decoder
        
        do {

if (self.iconImage != nil)
                {
                    if let gicon = weatherData.weather?.first?.icon {
                        DispatchQueue.main.async {
                            self.iconImage.image! =  gicon
                        }
                    }
                }
                if (self.LocationLabel != nil)
                {
                    if let gmain = weatherData.name {
                        print(gmain)
                        DispatchQueue.main.async {
                            self.LocationLabel.text! = "Current Weather in: " + String (gmain)
                        }
                    }
                }
                
                
                if (self.HumidityLabel != nil)
                {
                    if let ghumidity = weatherData.main?.humidity
                    {
                        print(ghumidity, "THIS IS HUMIDITY")
                        DispatchQueue.main.async {
                            self.HumidityLabel.text! = String (ghumidity)
                        }
                    }
                }
ios swift uiimage openweathermap
2个回答
0
投票

使用翠鸟

它在 ImageView 中创建一个扩展。您将使用

self.imageview.kf.setImage(with: "url")


0
投票

图标是图像的id,您需要将其附加到此url并加载它

http://openweathermap.org/img/w/10d.png // 这里 ------ id = 10d

假设您将使用 SDWebImage ,然后执行此操作

let urlStr = "http://openweathermap.org/img/w/\(gicon).png"
self.iconImage.sd_setImage(with: URL(string:urlStr), placeholderImage: UIImage(named: "placeholder.png"))

请参阅此处文档

© www.soinside.com 2019 - 2024. All rights reserved.