自动预热(预取)UITableView
和 UICollectionView
中的内容。
这个库类似于在 iOS 10 中添加的
UITableViewDataSourcePrefetching
和UICollectionViewDataSourcePrefetching
。
使用 Preheat
的一种方法是改善显示图像集合的应用程序的用户体验。Preheat
允许您检测哪些单元格即将显示在屏幕上,并为这些单元格预取图像。您可以使用 Preheat
与任何图像加载库一起使用,包括为其设计的 Nuke。
自动预热的思想受到了苹果 Photos 框架 示例应用 的启发。
以下是在您的应用程序中使用 Preheat 和 Nuke 实现预热的示例
import Preheat
import Nuke
class PreheatDemoViewController: UICollectionViewController {
let preheater = Nuke.Preheater()
var controller: Preheat.Controller<UICollectionView>?
override func viewDidLoad() {
super.viewDidLoad()
controller = Preheat.Controller(view: collectionView!)
controller?.handler = { [weak self] addedIndexPaths, removedIndexPaths in
self?.preheat(added: addedIndexPaths, removed: removedIndexPaths)
}
}
func preheat(added: [IndexPath], removed: [IndexPath]) {
func requests(for indexPaths: [IndexPath]) -> [Request] {
return indexPaths.map { Request(url: photos[$0.row]) }
}
preheater.startPreheating(with: requests(for: added))
preheater.stopPreheating(with: requests(for: removed))
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
controller?.enabled = true
}
override func viewDidDisappear(animated: Bool) {
super.viewDidDisappear(animated)
// When you disable preheat controller it removes all preheating
// index paths and calls its handler
controller?.enabled = false
}
}
将安装的模块导入源文件中
import Preheat
Preheat 在 MIT 许可下可用。有关更多信息,请参阅 LICENSE 文件。