在巨大的 UICollectionView 中加载图片
Loading pictures in a huge UICollectionView
我正在通过以下方式为我的 collection 视图单元格下载图片:
func loadThumbnail(..){
...
//Making the request
self.ProfilePicture.sd_setImage(with: requestUrl, placeholderImage: #imageLiteral(resourceName: "SharePreviewIcon"), completed: { [weak self] (newImage, error, cacheType, url) in
if self == nil {
return
}
if !isActive(){ //If the cell is not active, ignore and return
return
}
return
})
}
每次单元格处于活动状态时,我都会调用 loadThumbnail。现在如果我有 30 个单元格,并且有 15 个处于活动状态,那么将加载所有 15 张图片,并且很多请求正在被系统取消或简单地关闭,因为它们一次太多了。
我还是个外行,只是不知道该做什么。
非常感谢。
重要编辑*
一切都在选项卡控制器中,每个选项卡都有自己的 collection 视图。如果我在 prepareForUse() 方法中取消下载,那么只有那些被回收的单元会被取消。
如果我切换选项卡,仍处于活动状态的单元格不会被取消,而且我无法从新的 collection 视图加载单元格。
您应该明白,在 CellForItem
中您可以重复使用该单元格,因此您在一个单元格上启动的请求可以在滚动时在同一单元格上再次调用。
考虑这样做 -
在 CellForItem
调用 loadTumbnail 时,这将像您一样加载图像。
在单元格覆盖内 - prepareForReuse
你应该在那里取消请求
func prepareForReuse() {
super.prepareForReuse()
imageView.cancelCurrentImageLoad; // UIImageView for whatever image you need to cancel the loading for
}
这将在开始另一个请求之前取消请求。
另一件事 - self.ProfilePicture,考虑将 imageView 名称更改为小写。
https://developer.apple.com/documentation/uikit/uicollectionreusableview/1620141-prepareforreuse
我正在通过以下方式为我的 collection 视图单元格下载图片:
func loadThumbnail(..){
...
//Making the request
self.ProfilePicture.sd_setImage(with: requestUrl, placeholderImage: #imageLiteral(resourceName: "SharePreviewIcon"), completed: { [weak self] (newImage, error, cacheType, url) in
if self == nil {
return
}
if !isActive(){ //If the cell is not active, ignore and return
return
}
return
})
}
每次单元格处于活动状态时,我都会调用 loadThumbnail。现在如果我有 30 个单元格,并且有 15 个处于活动状态,那么将加载所有 15 张图片,并且很多请求正在被系统取消或简单地关闭,因为它们一次太多了。
我还是个外行,只是不知道该做什么。 非常感谢。
重要编辑*
一切都在选项卡控制器中,每个选项卡都有自己的 collection 视图。如果我在 prepareForUse() 方法中取消下载,那么只有那些被回收的单元会被取消。
如果我切换选项卡,仍处于活动状态的单元格不会被取消,而且我无法从新的 collection 视图加载单元格。
您应该明白,在 CellForItem
中您可以重复使用该单元格,因此您在一个单元格上启动的请求可以在滚动时在同一单元格上再次调用。
考虑这样做 -
在 CellForItem
调用 loadTumbnail 时,这将像您一样加载图像。
在单元格覆盖内 - prepareForReuse
你应该在那里取消请求
func prepareForReuse() {
super.prepareForReuse()
imageView.cancelCurrentImageLoad; // UIImageView for whatever image you need to cancel the loading for
}
这将在开始另一个请求之前取消请求。
另一件事 - self.ProfilePicture,考虑将 imageView 名称更改为小写。
https://developer.apple.com/documentation/uikit/uicollectionreusableview/1620141-prepareforreuse