快速从网址中获取网址后,将图片顺利填充到collectionview上

时间:2019-05-06 05:42:40

标签: ios swift alamofire alamofireimage

我有一个网址

 https://fotorusapi20161004041801.azurewebsites.net/api/fotorus/1

当我点击该网址时,我会得到一系列字典作为响应。

[{
    "Height" : 700,
    "PreviewPath" : "http:\/\/mobisoftframe.azureedge.net\/previewframe\/pre_pip132.jpg",
    "CategoryName" : [
      "PIP"
    ],
    "Width" : 450,
    "FramePath" : "http:\/\/mobisoftframe.azureedge.net\/frames\/pip132.png",
    "LayoutID" : 132,
    "Frames" : [
      {
        "X" : 99,
        "Y" : 307,
        "isBlur" : false,
        "Height" : 245,
        "MaskImagePath" : null,
        "Width" : 238
      }
    ],
    "FrameCount" : 1
  },
  {
    "Height" : 700,
    "PreviewPath" : "http:\/\/mobisoftframe.azureedge.net\/previewframe\/pre_pip133.jpg",
    "CategoryName" : [
      "PIP"
    ],
    "Width" : 450,
    "FramePath" : "http:\/\/mobisoftframe.azureedge.net\/frames\/pip133.png",
    "LayoutID" : 133,
    "Frames" : [
      {
        "X" : 26,
        "Y" : 272,
        "isBlur" : false,
        "Height" : 182,
        "MaskImagePath" : null,
        "Width" : 190
      }
    ],
    "FrameCount" : 1
  }
]

作为回应,我得到了100多个这样的字典。

获取响应的代码如下:-

//MARK: Api Calling



     func callApi(category:String,completion:@escaping (Bool)-> Void) {
        NetworkClass().callApiService(categoryCount:category) { (result,error)  in
          guard let response = result else {
            hideProgressHud(view:self.view)
            self.showalert(message:(error?.localizedDescription)!, title:"")
            completion(false)
            return
          }

          let parsedPIPResponse = populatePIPResponse(jsonData: response)
          selectedCategoryPreviewPath.removeAll()
          for objects in parsedPIPResponse{
            selectedCategoryPreviewPath.append(objects.PreviewPath!)
          }
          completion(true)
        }
      }

//Now I am populating the collectionview as follows:-

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
      let cell = collectionView.dequeueReusableCell(withReuseIdentifier:"categoryHolderCVCell", for:indexPath)  as! CategoryHolderCVCell

      collectionView.contentInset = UIEdgeInsets(top:50, left: 0, bottom: 50, right: 0)
      let cell = collectionView.dequeueReusableCell(withReuseIdentifier:"frameCVCell", for:indexPath)  as! FrameCVCell
      let url = URL.init(string: selectedCategoryPreviewPath[indexPath.row])
      cell.frameCategoryImageView.af_setImage(
        withURL: url!,
        placeholderImage: nil,
        filter: nil,
        imageTransition:.noTransition,
        completion: { response in
      }
      )
      return cell

  }

在选择行时,我正在按以下方式管理数据:-

 func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
      showProgressHud(view:self.view)

      callApi(category:String(indexPath.row+1)) { (success) in
        if(success){
          self.sliderHorizontalConstraint.constant = self.sliderView.frame.size.width*CGFloat(indexPath.item)
          UIView.animate(withDuration:0.75, delay:0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
            self.view.layoutIfNeeded()
          }, completion:nil)

          let cell = collectionView.cellForItem(at: indexPath) as! CategoryCVCell
          cell.tintColor = UIColor.blue

          self.selectedIndexPathRow = indexPath.row
          self.categoryCollectionView.reloadData()

          self.categoryHolderCollectionView.reloadData()
          hideProgressHud(view:self.view)
        }
      }

  }

这整个任务耗时很多。我需要帮助减少时间并提高平滑度,当我调用api fetch imageurl并让用户等到它获取所有图像URL时,以便我可以使用这些URL获取缩略图。

这是我第一次实施。请帮助或指导以改善任务的性能。

任何帮助或指导都是可取的。谢谢!!

0 个答案:

没有答案