【问题标题】:How can I prefetch images with AlamofireImage using the uiprefetching delegate?如何使用 uiprefetching 委托使用 AlamofireImage 预取图像?
【发布时间】:2019-08-22 10:14:59
【问题描述】:

我正在尝试使用 AlamofireImage 为我的 tableView 实现 UITableViewDataSourcePrefetching 委托。但是我该如何设置呢?

我已经添加了委托,并在我的 viewDidLoad 中设置了:

self.tableView.prefetchDataSource = self

然后是相关方法:

func tableView(_ tableView: UITableView, prefetchRowsAt indexPaths: [IndexPath]) {
         indexPaths.forEach {}

func tableView(_ tableView: UITableView, cancelPrefetchingForRowsAt indexPaths: [IndexPath]) {
        indexPaths.forEach {}

在我的 cellForRowAt indexPath 方法中,我下载这样的图像:

let url = URL(string: car.fotoUrl)!

        cell.imageCar.af_setImage(
            withURL: url,
            placeholderImage: placeholderImage,
            imageTransition: .crossDissolve(0.2),
            runImageTransitionIfCached: false,
            completion: { response in
                if response.result.isSuccess {
                    self.dismissLabel()
                } else if response.error?._code == NSURLErrorNotConnectedToInternet{
                    self.showLabel()
                }

总结一下,我想在预取方法中预加载图像,以改善表格视图的滚动,而不是在每次呈现单元格时下载它们。

【问题讨论】:

    标签: swift uitableview alamofire prefetch


    【解决方案1】:

    UIImageView.af_sharedImageDownloader.downloadImages(URLRequests: 请求)

    在显示集合视图单元格之前将其放在某处。下次将从缓存中获取图像。

    【讨论】:

    • 我需要在 prefetchRowsAt 方法中设置这个吗?
    • “ImageDownloader”类型的值没有成员“downloadImages”
    • prntscr.com/n5x769 您可以在需要的地方预加载它。在会话开始时,前一个屏幕。与您描述的方法相同,但需要使用单个项目的下载方法。
    • @NoemiPecorari - 是的,在prefetchRowsAt 中,当然还有为您的表格视图设置prefetchDataSource。请注意,它只是 download(_:),而不是 downloadImages(URLRequests:)。在 AlamofireImages 4.x 中,它现在是 UIImageView.af.sharedImageDownloader.download(requests)(没有下划线)。
    猜你喜欢
    • 2017-01-19
    • 1970-01-01
    • 1970-01-01
    • 2013-11-26
    • 1970-01-01
    • 2016-02-02
    • 2018-02-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多