I have a url

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

When I hit this url I get array of dictionaries as response.

[{
    "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
  }
]

I get more than 100 dictionaries like this in response.

Code for fetching response is as follows:-

//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

  }

On Selection of rows ,I am managing Data as follows:-

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

  }

This whole task is taking a lot time.I need help to reduce time and improve smoothness when I call api fetch imageurl and make user wait until it fetches all the image url ,so that i can use those urls to get thumbnail image.

I am implementing this first time.Kindly help or guide to improve the performance of task.

Any help or guidance would be appreciable .Thanks in advance!

0 Answers