滚动条上的主线程阻塞和 Kingfisher 设置图像
Main thread blocking on scroll and Kingfisher setting the image
我一直 运行 降低滚动性能,我注意到当我滚动并且 setImage
被调用时使用非缓存图像,下载发生时性能滞后。
if let imageURL = URL(string: presentable.imageUrl) {
let resource = ImageResource(downloadURL: imageURL)
photoView.kf.setImage(with: resource, options: [.transition(.fade(0.2))])
}
我的理解是翠鸟在后台线程下载这些,然后在主线程上显示它们,但主线程似乎暂时被阻塞了。删除 .transition
对情况没有帮助。
关于如何提高我的滚动性能的任何想法?谢谢
当图像大于图像视图时,iOS 需要操作大型 UIImage
对象,这会导致 UI 中出现明显的卡顿现象。您可以通过在使用图像之前调整图像大小来防止出现该问题。
幸运的是,Kingfisher 有一个处理器可以为您调整这些(在后台线程中)的大小。正如 Cheat Sheet 所说:
Using DownsamplingImageProcessor
for high resolution images
Think about the case we want to show some large images in a table view or a collection view. In the ideal world, we expect to get smaller thumbnails for them, to reduce downloading time and memory use. But in the real world, maybe your server doesn't prepare such a thumbnail version for you. The newly added DownsamplingImageProcessor
rescues [sic]. It downsamples the high-resolution images to a certain size before loading to memory:
imageView.kf.setImage(
with: resource,
placeholder: placeholderImage,
options: [
.processor(DownsamplingImageProcessor(size: imageView.size)),
.scaleFactor(UIScreen.main.scale),
.cacheOriginalImage
])
Typically, DownsamplingImageProcessor
is used with .scaleFactor
and .cacheOriginalImage
. It provides a reasonable image pixel scale for your UI, and prevent future downloading by caching the original high-resolution image.
我用小图片创建了一个小测试并确认它如丝般顺滑,但是当我使用大图片时,我在滚动行为中遇到了卡顿。但是当我在大图场景下加上这个DownsamplingImageProcessor
,又是丝般顺滑了
Updating
self.imageView.contentMode = .scaleAspectFill
to
self.imageView.contentMode = .scaleAspectFit
did the trick for me.
我在 UITableViewCell
中嵌入了 UICollectionView
。每个 UICollectionViewCell
都有一个图像视图,它使用 Kingfisher 从 URL 加载图像。我的 table 视图滚动非常不稳定,我尝试了不同的方法,包括不将模型和设置数据绑定到每个单元格。但没有任何区别。一旦我更改了图像视图的内容模式,它现在可以非常流畅地滚动。
根据 Rob 的回答,即使图像尺寸较小(在我的情况下甚至是 480 x 320),也可能需要将模式设置为 .scaleAspectFill
才能进行一些处理。将模式更新为.scaleAspectFit
后,现在即使是大尺寸图片也能流畅滚动。
我一直 运行 降低滚动性能,我注意到当我滚动并且 setImage
被调用时使用非缓存图像,下载发生时性能滞后。
if let imageURL = URL(string: presentable.imageUrl) {
let resource = ImageResource(downloadURL: imageURL)
photoView.kf.setImage(with: resource, options: [.transition(.fade(0.2))])
}
我的理解是翠鸟在后台线程下载这些,然后在主线程上显示它们,但主线程似乎暂时被阻塞了。删除 .transition
对情况没有帮助。
关于如何提高我的滚动性能的任何想法?谢谢
当图像大于图像视图时,iOS 需要操作大型 UIImage
对象,这会导致 UI 中出现明显的卡顿现象。您可以通过在使用图像之前调整图像大小来防止出现该问题。
幸运的是,Kingfisher 有一个处理器可以为您调整这些(在后台线程中)的大小。正如 Cheat Sheet 所说:
Using
DownsamplingImageProcessor
for high resolution imagesThink about the case we want to show some large images in a table view or a collection view. In the ideal world, we expect to get smaller thumbnails for them, to reduce downloading time and memory use. But in the real world, maybe your server doesn't prepare such a thumbnail version for you. The newly added
DownsamplingImageProcessor
rescues [sic]. It downsamples the high-resolution images to a certain size before loading to memory:imageView.kf.setImage( with: resource, placeholder: placeholderImage, options: [ .processor(DownsamplingImageProcessor(size: imageView.size)), .scaleFactor(UIScreen.main.scale), .cacheOriginalImage ])
Typically,
DownsamplingImageProcessor
is used with.scaleFactor
and.cacheOriginalImage
. It provides a reasonable image pixel scale for your UI, and prevent future downloading by caching the original high-resolution image.
我用小图片创建了一个小测试并确认它如丝般顺滑,但是当我使用大图片时,我在滚动行为中遇到了卡顿。但是当我在大图场景下加上这个DownsamplingImageProcessor
,又是丝般顺滑了
Updating
self.imageView.contentMode = .scaleAspectFill
to
self.imageView.contentMode = .scaleAspectFit
did the trick for me.
我在 UITableViewCell
中嵌入了 UICollectionView
。每个 UICollectionViewCell
都有一个图像视图,它使用 Kingfisher 从 URL 加载图像。我的 table 视图滚动非常不稳定,我尝试了不同的方法,包括不将模型和设置数据绑定到每个单元格。但没有任何区别。一旦我更改了图像视图的内容模式,它现在可以非常流畅地滚动。
根据 Rob 的回答,即使图像尺寸较小(在我的情况下甚至是 480 x 320),也可能需要将模式设置为 .scaleAspectFill
才能进行一些处理。将模式更新为.scaleAspectFit
后,现在即使是大尺寸图片也能流畅滚动。