Harsha
Harsha

Reputation: 818

I am downloading Images from URL, but I did not get them

  func getImagesFromURL(){
    for i in 0...carData.count-1{
        let pictureUrl = URL(string: "\(carData[i].pictureUrl!)")
        if let url = pictureUrl{
            _ = URLSession.shared.dataTask(with: url){(data, reponse, error) in
                if error != nil{
                    print(error!)
                    print("error")
                }else{
                    DispatchQueue.main.async {
                        if let image = UIImage(data : data!){
                            self.pictures.append(image)
                            self.carListTableView.reloadData()
                        }else{
                            print("No image in data")
                        }
                    }   //DispatchQueue
                }
            } //task
        } //url
        else{
            print("No any url")
        }
    } //for loop
} //getImagesFromURL     

Here 'carData[].pictureUrl' consists of urls and pictures is an array of UIImage. I am accessing this pictures in tableView, but I am not getting the images(waiting for long time)

Upvotes: 0

Views: 35

Answers (1)

Malik
Malik

Reputation: 3802

You are missing .resume() at the end of your task block. Update your code to

_ = URLSession.shared.dataTask(with: url){...
    ...
    ...
    ...
}.resume()

Upvotes: 1

Related Questions