【问题标题】:SDWebImage caching is taking time to load the image from cache in uicollectionview cellSDWebImage 缓存需要时间从 uicollectionview 单元中的缓存加载图像
【发布时间】:2016-12-30 09:10:06
【问题描述】:

我正在使用 SDWebImage 库在集合视图中加载图像。

库从服务器和缓存下载图像,但是当我退出我的应用程序并重新运行时,从缓存加载图像需要时间。 (占位符图像显示 2-3 秒有时甚至更长)

使用 SDWebImage 加载图像的代码:

cell.imgPost.sd_setImage(with: URL(string: (post.lowQualityImgUrl) ?? ""), placeholderImage: UIImage(named:"greybg") , options: .refreshCached, progress: { (recievedSize, expectedSize) in

        progressIndicatorView.progress = CGFloat(recievedSize)/CGFloat(expectedSize)

        }, completed: { (image, error, cachetype, url) in

            if image != nil{
                DispatchQueue.main.async {
                    progresView.removeFromSuperview()
                    progressIndicatorView.removeFromSuperview()
                    cell.progressIndicatorView?.isHidden = true
                    cell.imgPost.image = image

                }
            }
    })

这里使用的缓存选项是刷新缓存。

问题: 我不知道为什么集合视图单元格不能使用 sdwebimage 立即加载图像。正如它所说 sdwebimage 是图像缓存的最佳库。 谁能指出解决方案/问题?

编辑 我确实在他们的图书馆留下了评论,我认为这从来都不是一个好的图书馆。 https://github.com/rs/SDWebImage/issues/138

提前致谢。

【问题讨论】:

  • 为什么要在块内使用cell.imgPost.image = image 再次设置图像。?它已经被这个 cell.imgPost.sd_setImage(with: 块设置了。
  • @Poles 如果我不放置 cell.imgpost.image = image 然后在滚动图像视图上再次显示占位符我不知道我错过了什么

标签: ios swift uicollectionview sdwebimage image-caching


【解决方案1】:

来自SDWebImage 文档,用于refreshCached

 * Even if the image is cached, respect the HTTP response cache control, and refresh the image from remote location if needed.
 * The disk caching will be handled by NSURLCache instead of SDWebImage leading to slight performance degradation.
 * This option helps deal with images changing behind the same request URL, e.g. Facebook graph api profile pics.
 * If a cached image is refreshed, the completion block is called once with the cached image and again with the final image.
 *
 * Use this flag only if you can't make your URLs static with embedded cache busting parameter.

如果您不想缓存图像,则不应使用refreshCached,除非您在同一 URL 中获得不同的图像。将其保留为默认设置,它将为您处理一切。

编辑:options 保留为空数组,[]。它应该继续使用默认设置。您的延迟可能是因为您的代码中仍有refreshedCache

cell.imgPost.sd_setImage(with: NSURL() as URL!, placeholderImage: UIImage(), options: [], progress: { (_, _) in

}, completed: { (_, _, _, _) in

})

【讨论】:

  • 问题是图像缓存正确。但是在重新启动时,从缓存加载需要时间(2-3 秒)延迟为什么要延迟这么多
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-14
  • 2012-10-13
  • 1970-01-01
  • 1970-01-01
  • 2012-11-30
  • 1970-01-01
相关资源
最近更新 更多