如何将由Kingfisher缓存下载的图像放入数组中?

4
这是我的Swift 3代码。我需要将通过Kingfisher缓存下载的图像放入一个数组中。实际上,图像现在显示在cell.itemImage中,但我无法将这些图像放入UIImage数组中。
有人能帮忙吗?
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
}

你想将图片添加到UIImage数组中有什么特别的原因吗? - kathayatnk
是的,我需要通过 prepare for segue 将图像传递到另一个类中。 - Wide Angle Technology
当用户点击单元格时,您是否想要传递此图像?如果是这种情况,那么您就不需要一个数组。 - Ramis
那么我该如何传递这个值呢? - Wide Angle Technology
2个回答

1
我认为只传递图片网址就可以了。你可以像下面这样做。
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)

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接