简体   繁体   English

Swift-慢速UITableView滚动(从设备存储中加载图像)

[英]Swift - Slow UITableView scroll (loading image from device storage)

I have a UITableView populated with cells either containing just a text label or a text label and a UIImage. 我有一个UITableView,其中填充了包含文本标签文本标签和UIImage的单元格。 When the app is run, images are downloaded from my server and stored on the device locally. 运行该应用程序时,图像将从我的服务器下载并本地存储在设备上。 Every time the app is run, the app checks for new images and downloads them. 每次运行该应用程序时,该应用程序都会检查并下载新图像。 In my cellForRowAtIndexPath function, I load the image from the device storage on a background thread then update the UI on the main thread. 在我的cellForRowAtIndexPath函数中,我将设备存储中的图像加载到后台线程中,然后更新主线程上的UI。

I don't know if I am doing this right but here is my code for displaying images inside my cellForRowAtIndexPath function: 我不知道我是否做对了,但是这是我的代码,用于在cellForRowAtIndexPath函数内部显示图像:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
        for image in self.postsImages {
            if ((image as! NSArray)[0] as! Int == postIDCurrent) {
                // there is an image associated with this post

                let postImage = UIImage(contentsOfFile: (currentArray.lastObject as? String)!)
                dispatch_async(dispatch_get_main_queue(), {
                    cell.postImageView.image = postImage
                    cell.postImageView.clipsToBounds = true
                    cell.postImageView.userInteractionEnabled = true
                    cell.postImageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(PostsTableViewController.imageTapped(_:))))
                })
            }
        }
    })

The for and the if statements both just really check if there is an image associated with the current cell we are going to display. forif语句实际上只是检查是否有与我们要显示的当前单元格关联的图像。

I'm sorry if I'm not providing sufficient information. 很抱歉,如果我没有提供足够的信息。 If you need anything more, please comment. 如果您还有其他需要,请发表评论。

Anyways, my question is, what am I doing wrong with this code which causes my UITableView to scroll very slow? 无论如何,我的问题是,此代码导致UITableView滚动速度非常慢,我在做什么问题?

Chances are that postImage is much larger than than the bounds of cell.postImageView, so the assignment on the main thread takes a noticeable amount of time to scale it down. 很有可能postImage比cell.postImageView的边界大得多,因此在主线程上进行分配会花费大量时间来缩小它。 Assuming that postImageView's bounds are foreseeable, scaling the image to the correct size in the background thread will be quite likely to sort this out. 假设postImageView的边界是可预见的,那么在后台线程中将图像缩放到正确的大小很有可能会解决这个问题。 If you don't have a resizing function handy, this gist looks reasonable . 如果您没有方便的调整大小功能,那么这个要点看起来很合理

Try breaking your for loop: 尝试中断for循环:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
    for image in self.postsImages {
        if ((image as! NSArray)[0] as! Int == postIDCurrent) {
            // there is an image associated with this post

            let postImage = UIImage(contentsOfFile: (currentArray.lastObject as? String)!)
            dispatch_async(dispatch_get_main_queue(), {
                cell.postImageView.image = postImage
                cell.postImageView.clipsToBounds = true
                cell.postImageView.userInteractionEnabled = true
                cell.postImageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(PostsTableViewController.imageTapped(_:))))
            })
            break
        }
    }
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM