简体   繁体   English

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

[英]How to get a UIImage from a String with JSON data

I am trying to load an icon image form the API OpenWeatherMap , and display it in an ImageView . 我正在尝试从API OpenWeatherMap加载图标图像,并将其显示在ImageView I am trying to load it into the imageView ' iconImage '. 我试图将其加载到imageViewiconImage ”。 I have successfully loaded the JSON data for the location and humidity, as they are Strings , but the Icon data is also a String and I cannot get it to display as a UIImage . 我已经成功加载了用于位置和湿度的JSON数据,因为它们是Strings ,但是Icon数据也是String ,因此无法将其显示为UIImage

Code below: 代码如下:

My JSON Structs below: 我的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`    

View controller below: 查看下面的控制器:

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

Icon is the id of the image , you need to append it to this url and load it 图标是图片的ID,您需要将其附加到此网址并加载

http://openweathermap.org/img/w/10d.png // here ------ id = 10d http://openweathermap.org/img/w/10d.png //此处------ id = 10d

suppose you'll use SDWebImage , then do this 假设您将使用SDWebImage ,然后执行此操作

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

See here in Docs 文档中查看

Use Kingfisher 使用翠鸟

It creates an extension in ImageView. 它在ImageView中创建扩展。 You will use self.imageview.kf.setImage(with: "url") 您将使用self.imageview.kf.setImage(with: "url")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM