2016-10-11 24 views
0

从Firebase加载图片并将其填充到单元格中时,似乎会出现波涛汹涌的表现。我认为这可能是因为图像没有被缓存。Collections从Firebase的网址加载图片时的查看性能不稳定

这个位于功能viewDidLoad中()

let databaseRef = FIRDatabase.database().reference() 

      databaseRef.child("palettes").queryOrdered(byChild: "top").queryEqual(toValue: "#000000").observeSingleEvent(of: .value, with: { (snapshot) in 


       if let snapDict = snapshot.value as? [String:AnyObject]{ 

        for each in snapDict as [String:AnyObject]{ 

         let URL = each.value["URL"] as! String 
         self.URLArray.append(URL) 

         print(self.URLArray) 
        } 
       } 
      }) 

后立即从返回的搜索结果中收集URL而这里的填充细胞的功能:

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) 
     let textLabel = cell.viewWithTag(2) 

     let ootdImage = cell.viewWithTag(4) as! UIImageView 

     if let url = NSURL(string: self.URLArray[indexPath.row]) { 
      if let data = NSData(contentsOf: url as URL){ 
       ootdImage.image = UIImage(data: data as Data) 
      } 
     } 

     // display images from the dataArray. 
     textLabel?.backgroundColor = averageColor 

     return cell 

不要担心约collectionsView.reloadData(),目前,我已经设置它,使得我必须手动点击一个按钮来重新加载数据,但我仍然不明白为什么滚动存在滞后,特别是因为所有的数据已经被收集。

回答

1

性能低下,因为您正在从主线程下载每个图像,这会异步阻塞UI.Download图像并更新集合视图单元,这将解决性能问题。

当您滚动时,与主单元中获取的单元格关联的每个图像将导致laggy滚动。

+0

谢谢你的工作,我设置了这样的处理下载和数据转换为UIImage是在一个单独的功能。但是现在下载和显示图像仍然存在滞后性。 –