Ios 在UITableView中快速滚动图像
所以我已经阅读了很多关于这个问题的解决方案,似乎无论我做什么,当我的单元格中出现图像时,我仍然会在UITableView中快速滚动 这里有一些关于我是如何生成细胞的信息 我正在计算单元格的高度,并在索引路径处缓存行的高度Ios 在UITableView中快速滚动图像,ios,swift,uitableview,swift3,sdwebimage,Ios,Swift,Uitableview,Swift3,Sdwebimage,所以我已经阅读了很多关于这个问题的解决方案,似乎无论我做什么,当我的单元格中出现图像时,我仍然会在UITableView中快速滚动 这里有一些关于我是如何生成细胞的信息 我正在计算单元格的高度,并在索引路径处缓存行的高度 func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { if let height = cachedHeights[indexPath.r
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if let height = cachedHeights[indexPath.row] {
return height
} else {
let post = dataSource.items[indexPath.row]
var CellClass = FeedTableViewCell.self
if let RegisteredCellClass = cells[post.reusableIdentifier] {
CellClass = RegisteredCellClass
}
cachedHeights[indexPath.row] = CellClass.height(post)
return CellClass.height(post)
}
}
我已经验证了实际尺寸和计算尺寸是相同的
在indexpath为行配置单元格中的单元格时,我设置了所有元素,并使用SDWebImage异步加载图像
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.section == 1 {
let post = dataSource.items[indexPath.row]
var wallCell: FeedTableViewCell?
if let registeredCell = tableView.dequeueReusableCell(withIdentifier: post.reusableIdentifier) as? FeedTableViewCell {
wallCell = registeredCell
if let postCell = wallCell as? FeedTableViewPostCell {
postCell.informationDelegete = self
postCell.actionDelegate = self
}
}
guard let cell = wallCell else { return FeedTableViewPostCell() }
cell.configureCell(post)
cell.delegate = self
return cell
} else {
return UITableViewCell()
}
}
Configure cell在单元格上调用用post数据填充元素的方法。有一个子视图称为mediaview,用于处理图像。如果在post中有图像,它们将在该视图中进行配置,如下所示
for (index, element) in media.enumerated() where index < 3 {
addSubview(viewsArray[index])
viewsArray[index].setImage(with: element.source, placeholderImage: nil)
}
如果我在mediaview中注释掉设置图像的块,我的滚动非常平滑,因此我知道这不是细胞生成的另一部分。我的理解是异步加载应该可以缓解滚动延迟,但我尝试了几乎所有的方法都没有效果。如果您对此有任何帮助或见解,我们将不胜感激。您是否尝试过运行探查器?@Alexander我已经运行了core animation profiler,在滚动时,我看到帧速度下降到55 FPS左右。不是很大的下降,但是当图像单元格加载时会有一个明显的停顿。你在缓存图像吗?还有什么叫setImage?是的,使用SDWebImages缓存机制缓存图像。在cellForRowAt中配置单元格时,会在单元格的“媒体视图”子视图上调用setImage。您是否尝试运行探查器?@Alexander我已运行core animation profiler,在滚动时,我看到帧下降到大约55 FPS。不是很大的下降,但是当图像单元格加载时会有一个明显的停顿。你在缓存图像吗?还有什么叫setImage?是的,使用SDWebImages缓存机制缓存图像。在cellForRowAt中配置单元格时,在单元格的媒体视图子视图上调用setImage。
func setImage(with url: URL?, placeholderImage: UIImage?){
if let placeholder = placeholderImage{
DispatchQueue.main.async(execute: {
self.image = placeholder
})
}
SDWebImageManager.shared().loadImage(with: url, options: [SDWebImageOptions.cacheMemoryOnly, SDWebImageOptions.scaleDownLargeImages], progress: nil, completed: {(image, data, error, cacheType, finished, url) in
if finished {
DispatchQueue.main.async(execute: {
self.alpha = 0
UIView.transition(with: self, duration: 0.1, options: UIViewAnimationOptions.transitionCrossDissolve, animations: { () -> Void in
self.image = image
self.alpha = 1
}, completion: nil)
})
}
})
}