问题描述
我有一个应用程序,它获取 RSS 提要,然后使用这些提要中的图像 URL 下载和填充 ImageView。我尝试了实现 iOS 预取(请参阅下面的代码),但它仍然有问题,就像滚动时它会短暂暂停(即使以“合理的速度滚动”)。我不确定调试它的最佳方法是什么 - 有什么建议吗?
这是 TableViewController 的 cellForRowAt
:
override func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "NewsTableViewCell",for: indexPath) as! NewsTableViewCell
if let item = RSSItems?[indexPath.item] {
cell.item = item
cell.selectionStyle = .none
cell.descriptionText.text = item.isExpanded ? item.description : ""
cell.descriptionText.isHidden = item.isExpanded ? false : true
cell.delegate = self
if let imageView = cell.viewWithTag(100) as? UIImageView {
if (RSSItems![indexPath.row].imageData != nil) {
imageView.contentMode = .scaleAspectFill
imageView.image = UIImage(data: item.getimageData()!)
} else {
imageView.image = nil
if cell.item.imageLink == "" {
imageView.contentMode = .scaleAspectFit
imageView.image = UIImage(named:item.logoImage!)
} else {
if let imageUrl:URL = URL(string: cell.item.imageLink!) {
cell.item.imageData = try? Data(contentsOf: imageUrl)
imageView.image = UIImage(data: cell.item.imageData!)
}
}
}
}
}
return cell
}
这里是预取扩展(我没有实现取消下载部分):
// MARK: - UITableViewDataSourcePrefetching
extension NewsTableViewController: UITableViewDataSourcePrefetching {
func tableView(_ tableView: UITableView,prefetchRowsAt indexPaths: [IndexPath]) {
// print("prefetchRowsAt \(indexPaths)")
indexPaths.forEach { self.RSSItems![$0.row].setimageData() }
}
func tableView(_ tableView: UITableView,cancelPrefetchingForRowsAt indexPaths: [IndexPath]) {
// print("cancelPrefetchingForRowsAt \(indexPaths)")
// indexPaths.forEach { self.cancelDownloadingImage(forItemAtIndex: $0.row) }
}
}
然后,setimageData
函数来自一个类,该类构成一个对象,该对象表示一个提要项以及给定单元格的图像的 URL:
func setimageData() {
if self.imageLink != nil {
guard let imageUrl:URL = URL(string: self.imageLink!) else {
return
}
guard let imageData = try? Data(contentsOf: imageUrl) else {
return
}
self.imageData = imageData
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)