使用 UIImage 滚动时表格视图单元格滞后

万吉罗

UIImage 被加载,在滚动通过 1 个单元格后,它会滞后并继续其他单元格。

是因为电池可重复使用吗? 在此处输入图片说明

extension TableViewController: UITableViewDelegate, UITableViewDataSource {

        func numberOfSections(in tableView: UITableView) -> Int {
            return 1
        }

        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return post.count
        }

        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = UITableViewCell(style: .default, reuseIdentifier: "myCell")
            DispatchQueue.main.async {

                //MARK:- UIImage as URL
                let postImage = self.post[indexPath.row]
                if let photoUrl = URL(string: postImage.photoUrl!)
                {
                    do {
                        let data = try Data(contentsOf: photoUrl)
                        cell.imageView?.image = UIImage (data: data)
                        cell.imageView?.frame = CGRect(x: 0, y: 0, width: 450, height: 450)

                    } catch {
                        print("")
                    }
                }// postImage
            }
            return cell
        }

    } //MARK:- Extension into Table View Controller
安东·菲利莫诺夫

它滞后是因为您在主线程上进行“繁重”操作。DispatchQueue.main.async 在这里没有帮助,因为它仍然在主线程中执行代码。你真的必须在主线程上做这两行代码:

cell.imageView?.image = UIImage (data: data)
cell.imageView?.frame = CGRect(x: 0, y: 0, width: 450, height: 450)

实际上,您甚至不需要第二个,因为布局应该由单元格本身完成(例如,您可以通过约束或自动调整大小掩码设置它)

The heaviest operation in your code is let data = try Data(contentsOf: photoUrl) because it performs data loading from network. So your main goal is to perform this operation out of main thread. For example you can do it like this:

DispatchQueue.global().async {
    do {
        let data = try Data(contentsOf: photoUrl)
        let image = UIImage(data: data) // this also could be pretty much heavy if the image is big so since we're already in background let's do it here
        DispatchQueue.main.async {
            imageView.image = UIImage (data: data)
        }
    }
    catch {
        print("")
    }
}

This example is pretty much basic and still has unsolved problems. For example, if image is very large than scaling it when it's set to imageView also could be expensive, so you'd better also scale image to imageView size in background thread. Also cells are reusable so when one image is loaded the cell could already be used for displaying another image so you also need a way to handle it. Also you'll probably want to cache loaded images to avoid reloading them from network every time on scroll.

所以这里有很多工作要做,为了让它更容易,我建议使用像Kingfisher这样的库(而不是作为例子的广告)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用最小高度的单元格自动调整表格视图

使用大量UIImage时出现内存问题

使用UI测试滚动单元格

UIImage方面使用drawInRect时填充?

如何使用自动布局来调整表格视图单元格中视图的大小?

无法使用解析在滚动视图中缩放UIImage

共享我编辑的UIImage时,UIActivityViewController崩溃,但可与UIImage(named :)一起使用

滚动后重复使用单元格后,如何在表格视图单元格中保持值?

使用委托显示/隐藏表格视图单元格

使用UISwitch显示/隐藏表格视图单元格

如何使用SwiftUI视图代替表格视图单元格

RxSwift:如何使用ViewModel在表格视图内的集合视图单元格中填充数据?

无法使用可滚动单元格将表格单元格内容居中

使用SDWebImage时表视图滚动滞后

使用UIScrollView滚动UIImage

在表格单元格中单击时使用UIActionsheet

快速单击单元格时,使用新的数组元素重新填充表格视图

使用Swift隐藏隐藏的静态表格视图单元格

使用嵌套表格单元格时的怪异行为

使用动态单元格高度时,将表格视图滚动到底部

使用UIView的自定义表格视图单元格

使用SearchBar时,表格视图单元格中的图像

在表格视图中滚动时图像重复单元格

在 Swift 中使用 Parse 删除表格视图单元格

在单元格中着色 UIImage - ObjectiveC

由于表格视图中可重复使用的单元格,当它滚动出视图时丢失 Youtube 播放器

使用堆栈视图将图像添加到表格视图单元格问题

你如何为表格视图单元格制作和使用 XIB 文件,而不是仅仅在故事板的表格视图中使用单元格?

对表格视图和集合视图使用相同的单元格