iOS - Collectionview images disappear on scrolling and sometimes overlaps

懵懂的女人 提交于 2019-12-25 11:52:45

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!