问题
When downloading images from server and populating the collection view (grid 3 * n). I am getting glitch .
I tried almost everything possible like making image view nil before reusing cell and using GCD for updating image view from main thread.
But I'm still facing the glitch.
Glitch is when I scroll the collection view , images downloaded are overlapped and sometimes disappear from the collection view cell.
I have no idea what is causing the image to disappear (which were once downloaded).
Any Suggestions.
private func cellForDropbox(cell:GridCell,indexPath:IndexPath) -> GridCell {
let filename = self.filenames[(indexPath as NSIndexPath).row]
cell.imageView.backgroundColor = UIColor.lightGray
cell.imageView.image = nil
DropboxClientsManager.authorizedClient?.files.getTemporaryLink(path: filename).response(completionHandler: { (response, error) in
if let url = response {
cell.imageView.sd_setImage(with: URL(string:url.link), placeholderImage: nil, options: .refreshCached, completed: { (img, error, cacheType, url) in
})
} else {
print("Error downloading file from Dropbox: \(error!)")
}
})
return cell
}
回答1:
Try this if do not want completion callback of image download.
private func cellForDropbox(cell:GridCell,indexPath:IndexPath) -> GridCell {
let filename = self.filenames[(indexPath as NSIndexPath).row]
cell.imageView.backgroundColor = UIColor.lightGray
DropboxClientsManager.authorizedClient?.files.getTemporaryLink(path: filename).response(completionHandler: { (response, error) in
if let url = response {
cell.imageView.sd_setImage(with: URL(string: url.link), placeholderImage: UIImage(named: "placeholder.png"))
} else {
print("Error downloading file from Dropbox: \(error!)")
}
})
return cell
}
来源:https://stackoverflow.com/questions/43890727/ios-collectionview-images-disappear-on-scrolling-and-sometimes-overlaps