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
这里'carData [] .pictureUrl'由网址组成,图片是UIImage的数组。我在tableView中访问这些图片,但我没有得到图像(等待很长时间)
答案 0 :(得分:0)
您在任务栏结束时遗漏了.resume()
。将代码更新为
_ = URLSession.shared.dataTask(with: url){...
...
...
...
}.resume()