4

This is my Swift 3 code. I need to place the images into an array which is downloading through Kingfisher cache. Actually, the image is now displaying in the cell.itemImage, but I am not able to put those images to a UIImage array.

Can anyone help?

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! ViewItemsCVC
    let Currency = UserDefaults.standard.string(forKey: "currency")
    cell.NameLabel.text = self.itemName[indexPath.row]
    cell.PriceLabel.text = "\(Currency ?? "INR")  \(self.itemPrice[indexPath.row])"
    let resource = ImageResource(downloadURL: URL(string: self.FullLink[indexPath.row])!, cacheKey: self.FullLink[indexPath.row])
    cell.itemImage?.kf.setImage(with: resource)
    return cell
}
Pang
  • 9,564
  • 146
  • 81
  • 122
Wide Angle Technology
  • 1,184
  • 1
  • 8
  • 28

2 Answers2

1

I believe passing only the imageURl will do. you can do something like below

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let currentImageUrl = self.FullLink[indexPath.row]
    performSegue(withIdentifier: "YOU_SEGUE_IDENTIFIER", sender: currentImageUrl)
}


override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "YOUR_SEGUE_IDENTIFIER" {
        if let desinationController = segue.destination as? UIViewController {

            if let imageURL = sender as? String {
                 //make imageURL variable in destination controller
                desinationController.imageURL = imageURL
            }
        }
    }
}

//then on next controller you only have to set the imageviews with the url, since kingfisher will cache them, there will be no need to pass whole UIImage, just the link will suffice

yourcontroller.imageView.kf.setImage(with: imageURL)
kathayatnk
  • 975
  • 1
  • 7
  • 9
1
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    switch segue.destination {
    case let viewController as YourViewController:
        let row = collectionView.indexPathsForSelectedItems?.first?.row
        let image = ImageResource(downloadURL: URL(string: self.FullLink[row])!, cacheKey: self.FullLink[row])
        viewController.image = image

    default:
        break
    }
}
Ramis
  • 13,985
  • 7
  • 81
  • 100