如何快速显示来自网址的图片

时间:2018-08-14 16:16:40

标签: ios swift

我正在进行一个异步调用,该响应的JSON响应同时包含要显示在UIImageView中的图像的文本和URL。现在,在标签中显示文本不是问题,但是加载图像给我带来了麻烦。在下面共享了我的代码。

//Declared variables to hold response
    var offerContent: String?
    var offerImage: URL?
    var offerTitle: String?
    var suggestionLabel:String?
    var suggestionScreen: String?



//create a task to send reques
            let task = URLSession.shared.dataTask(with: request as URLRequest){
                data, response, error in
                if error != nil {
                    print("error is:: \(error!.localizedDescription)")
                    return;
                }
                //parsing the response
                do {
                    // converting response to NSDictionary
                    let myJSON = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary

                    DispatchQueue.main.async {
                        if let parseJSON = myJSON{
                            var responseCode: Int!
                            var responseMessage: NSArray!

                            //getting the json response
                            responseCode = parseJSON["RESPONSECODE"] as! Int?
                            responseMessage = parseJSON["RESPONSEMESSAGE"] as! NSArray?

                            if responseCode == 0 {
                                for obj in responseMessage{
                                    if let dict = obj as? NSDictionary{
                                        self.offerContent = dict.value(forKey: "CONTENT") as? String
                                        self.offerTitle = dict.value(forKey: "TITLE") as? String
                                        self.suggestionLabel = dict.value(forKey: "SUGGESTION_LABEL") as? String
                                        self.suggestionScreen = dict.value(forKey: "SUGGESTION_SCREEN") as? String
                                        self.offerImage = dict.value(forKey: "IMAGE") as? URL
//                                        print("image:: \(self.offerImage!)")

                                        let offerImageView = UIImageView()
                                        self.darkView.addSubview(offerImageView)
                                        offerImageView.heightAnchor.constraint(equalToConstant: 30).isActive = true
                                        offerImageView.widthAnchor.constraint(equalTo: self.darkView.widthAnchor).isActive = true
                                        offerImageView.topAnchor.constraint(equalTo: self.darkView.topAnchor, constant: 20).isActive = true
                                        offerImageView.image = UIImage(data: self.offerImage)
                                    }
                                }
                            }

                            print(parseJSON)
                        }
                        }
                }catch{
                    print("catch:: \(error.localizedDescription)")
                }

            }
            task.resume()

2 个答案:

答案 0 :(得分:5)

尝试此代码。

'<ng-content select="ion-select,ion-input,ion-textarea,ion-datetime,ion-range,[item-content]"></ng-content>' +

答案 1 :(得分:0)

导入并使用Kingfisher

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