Я использовал SDWebImage
и cell.Food.sd_setImage(with: URL(string: foodImage[indexPath.row]))
для отображения изображений, а массив foodImage содержит URL.
Когда я запускаю его и получаю ошибки, как показано ниже. Я понятия не имею, как это исправить, и я не могу найти никаких ошибок по этому поводу. Кто-нибудь знает как это исправить? Спасибо.
Task <D58DD2B3-88A0-4673-BF27-A7AC5A65F3B4>.<3> finished with error - code: -999
...
Task <340CEBFF-ADE8-4C07-851D-0BD1F0159F20>.<8> finished with error - code: -999
Failed to get TCPIOConnection in addInputHandler
Task <5C16CD7B-2234-4BB6-BE2A-0D71ECD35658>.<12> finished with error - code: -999
[] nw_endpoint_flow_attach_protocols_block_invoke [4.1 107.20.173.119:443 in_progress socket-flow(satisfied)] Failed to attach application protocol CFNetworkConnection
Вот мой код:
class MainPageController: UIViewController, UICollectionViewDelegate,UICollectionViewDataSource{
public var foodTitle = [String]()
public var foodImage = [String]()
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
collectionView.dataSource = self
collectionView.delegate = self
DispatchQueue.main.async {
collectionView.reloadData()
}
return foodImage.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! MainPageCollectionViewCell
cell.FoodTitle.text = self.foodTitle[indexPath.item]
cell.Food.sd_setImage(with: URL(string: foodImage[indexPath.row]),placeholderImage: UIImage(named: "image")) // I tried your suggestion but it still failed.
return cell
}
extension MainPageController{
public func fetchFoodList(){
let url = URL(string: "https://api.edamam.com/search?q=egg&app_id=110d8671&app_key=3f01522208d512f592625dfcd163ff5c&from=0&to=10")
let task = URLSession.shared.dataTask(with: url!){ (data, response, error) in
if error != nil{
print(error!)
}else{
if let urlContent = data{
do{
let json = try JSON(data:data!)
let recipes = json["hits"]
self.foodTitle=json["hits"].arrayValue.map{$0["recipe"]["label"].stringValue}
print(self.foodTitle)
self.foodImage = json["hits"].arrayValue.map {$0["recipe"]["image"].stringValue}
print(self.foodImage)
}
catch{
print("JSON Processing Failed")
}
}
}
}
task.resume()}}