4

I'm making an async call which the JSON response contains both text and url to images that I want to display in a UIImageView. Now displaying the text in a label isn't the problem but loading the images is giving me hard times. Have shared my code below.

//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()
6
  • Is your dict.value(forKey: "IMAGE") content an url ? Commented Aug 14, 2018 at 16:17
  • you can use sdwebimage for displaying image url Commented Aug 14, 2018 at 16:31
  • @GIJOW yes it contains url. So i have edited my codes to make it url Commented Aug 14, 2018 at 16:31
  • 1
    Possible duplicate of Loading/Downloading image from URL on Swift Commented Aug 14, 2018 at 16:33
  • You can refer to this post: stackoverflow.com/questions/24231680/… also, you're handling your url as data (binary data) UIImage(data: self.offerImage). It will never work. Commented Aug 14, 2018 at 16:34

2 Answers 2

12

Try this code.

let url = URL(string: "IMAGE URL HERE")
let data = try? Data(contentsOf: url)

if let imageData = data {
    let image = UIImage(data: imageData)
}
Sign up to request clarification or add additional context in comments.

1 Comment

What if I want to send some headers with the request?
-1

Import and use Kingfisher

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.