There seems to be choppy performance when loading images from Firebase and then populating it into the cells. I figure it could be because the images are not cached.
This is located in the function to gather URL from the returned search results immediately after 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)
}
}
})
And here's the function to populate the cells:
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
Don't worry about collectionsView.reloadData()
, currently, I've set it such that I have to manually tap a button to reload data, but I still don't understand why there's a lag in the scrolling, especially since all the data has already been gathered.