繁体   English   中英

如何通过预取将图像从Firestore / Storage加载到collectionview?

[英]How to load images from Firestore/Storage into collectionview with prefetching?

我想将存储在Firestore中文档字段中的imageURLs中的图像加载到collectionview中,并在向下滚动collectionview时进行平滑滚动。 我看到collectionview预取可以做到这一点,但是找不到Firebase的示例。 使用我当前的代码,将获取图像,但是不会创建新的单元格。

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

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

    let vibration = self.vibrationsArray[indexPath.item]

    let thumbnailURL = URL(string: vibration.thumbnailURL!)
    cell.myImage.kf.setImage(with: thumbnailURL)

    return cell

}


func collectionView(_ collectionView: UICollectionView, prefetchItemsAt indexPaths: [IndexPath]) {

    if currentlyPrefetching == true {
        return
    }else {
        currentlyPrefetching = true

        if self.prevDoc != nil{
            self.vibrationsRef.limit(to: 3).start(afterDocument: self.prevDoc!).getDocuments { (snapshot, error) in

                if snapshot != nil {

                    self.prevDoc = snapshot?.documents.last
                    for document in snapshot!.documents{
                        let vibrationStringURL = document.get("thumbnail")

                        if vibrationStringURL != nil {
                            let vibration = Vibration()
                            vibration.thumbnailURL = (vibrationStringURL as! String)
                            self.vibrationsArray.append(vibration)
                        }
                    }
                }
                collectionView.reloadData()
                self.currentlyPrefetching = false
            }
        }

    }
}

我希望新单元格加载预取的imageURLStrings。 虽然cellForItemAt可以为我处理,但是不会使用预取的数据创建新的单元格。 我怎样才能解决这个问题?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM