arrays - 使用 SDWebImage 时 UICollectionView 错误

标签 arrays swift uicollectionview swift4 sdwebimage

我使用 SDWebImagecell.Food.sd_setImage(with: URL(string: foodImage[indexPath.row])) 来显示图像, foodImage 数组包含网址。

当我运行它并收到如下错误时。我不知道如何修复它,也找不到任何相关错误。有谁知道如何修理它?谢谢。

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()}}

最佳答案

我正在使用 sd_WebImage,它通过以下代码为我工作:-

  import SDWebImage

  func collectionView(_ collectionView: UICollectionView, cellForItemAt 
     indexPath: IndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", 
        for: indexPath) as! stackCollectionViewCell

     cell.imageview.sd_setImage(with: URL(string: imageArr[indexPath.row]), 
     placeholderImage: UIImage(named: "image"))


    return cell
  }

但是您的错误是针对线程的,因为某些东西正在中断主线程,这就是出现此错误的原因。那么你可以分享你的代码吗?

谢谢

关于arrays - 使用 SDWebImage 时 UICollectionView 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60028031/

相关文章:

ios - Swift 4,加载下一个 ViewController

objective-c - 来自钥匙串(keychain)的 IKEv2 密码引用

c - 如何释放在C中作为函数参数传递的字符数组

ios - UIImagePickerController 和 fatal error : array element cannot be bridged to Objective-C

ios - 如何实现滑动时从左向右移动的水平堆叠卡片?

ios - UICollectionView 滚动 SWIFT 滞后/缓慢

ios - UICollectionViewController 中的 collectionView 可选

c++ - 不准确的 C++ 阶乘程序

javascript - 如何从对象数组中获取数组本身属性的所有唯一值

ios - 将 header 添加到 UICollectionView,而不是 UICollectionView 的部分