简体   繁体   English

加载图像时提高UITableView滚动性能

[英]Improve UITableView scrolling performance while loading images

I have a scrolling grid of photos that looks and functions pretty much exactly like the photo picker on the iPhone. 我有一个滚动的照片网格,其外观和功能与iPhone上的照片选择器非常相似。 It is constructed from a UITableView that uses a custom UITableViewCell which displays a row of photos (very similar to how Three20 implements it). 它由使用自定义UITableViewCell的UITableView构造而成,该UITableViewCell显示一行照片(非常类似于Three20实现它的方式)。

It works great except that scrolling performance is poor. 它的工作原理很好,但滚动性能很差。 I'm already following most of the best practices for fast UITableView scrolling (à la Tweetie ). 我已经遵循了大多数快速UITableView滚动的最佳实践(la Tweetie )。

The images are all bundled with the app. 图像都与应用程序捆绑在一起。 I load them on the UI thread, on demand. 我根据需要将它们加载到UI线程上。 I use UIImage's imageNamed: so that the images will be cached after they're first loaded. 我使用UIImage的imageNamed:,以便在第一次加载图像后将其缓存。 Once I've scrolled through the table view once, it scrolls very smoothly. 一次在表格视图中滚动后,它会非常平滑地滚动。

The problem is, the first time scrolling through the table view, scrolling is jittery. 问题是,第一次滚动浏览表格视图时,滚动会产生抖动。 I've profiled the app and found that the majority of the time is spent loading the images from the file system. 我已经分析了该应用程序,发现大部分时间都花在了从文件系统加载图像上。 They are JPEG images, already sized correctly (small). 它们是JPEG图像,尺寸正确(较小)。 I tried using PNG images instead, but performance doesn't improve very much. 我尝试使用PNG图片代替,但是性能并没有太大提高。

The iPhone photo picker exhibits much better loading performance. iPhone照片选择器具有更好的加载性能。 I'm wondering if combining all the photos into a single image, which I load once and then split into smaller images would be faster. 我想知道是否将所有照片组合到一个图像中,将其加载一次然后分成较小的图像会更快。 It certainly works in games, but I know that's really a totally different story. 它当然可以在游戏中使用,但是我知道那是完全不同的故事。 Has anyone had experience doing that? 有没有人有这样做的经验?

Any other ideas for how I can improve performance? 关于如何提高性能的其他想法?

Incidentally, I'm having a similar, albeit less, performance problem for another UITableView that just uses standard UITableViewCells with one image assigned to the imageView per row. 顺便说一句,对于另一个只使用标准UITableViewCells且每行分配一个图像给UIView的UITableView,我也遇到了类似的(尽管较少)性能问题。

One thing to try could be pre-caching all the images when the view loads. 可以尝试的一件事是在视图加载时预缓存所有图像。 Beyond that, perhaps loading the images in the background (even though it's loading from the filesystem and not the web). 除此之外,也许在后台加载图像(即使是从文件系统而不是从网络加载)。 I haven't tried this myself, but perhaps you could use something like https://github.com/rs/SDWebImage and have the URLs point to the filesystem. 我自己还没有尝试过,但是也许您可以使用类似https://github.com/rs/SDWebImage的东西并使URL指向文件系统。 Users might see some placeholder images at first, but then the UI wouldn't stutter while images are loading. 用户最初可能会看到一些占位符图像,但是在加载图像时,UI不会结结巴巴。

I had a similar issue using a UITableView to display information parsed from an RSS Feed. 使用UITableView显示从RSS Feed解析的信息时,我遇到了类似的问题。 I ran into scrolling performance issues when there was a significant amount of data. 当有大量数据时,我遇到了滚动性能问题。 Though I'm still working on it I suspend the parsing when the table is being scrolled. 尽管我仍在处理它,但是在滚动表时我暂停了解析。 It resumes when scrolling stops. 滚动停止时恢复。 I am not at my computer, but I believe I used tableViewDidScroll and tableViewDidEndDecelerating . 我不在电脑tableViewDidScroll ,但我相信我使用了tableViewDidScrolltableViewDidEndDecelerating You can check these tableview delegates. 您可以检查这些tableview委托。 In any case it works very well, I just need a more elegant way to pause my parsing. 无论如何,它都非常有效,我只需要一种更优雅的方法来暂停我的解析即可。

将PagingEnabled属性设置为off可以提高滚动性能。

Fantastic. 太棒了 That does work like a charm! 这确实像一个魅力! So efficiently I ended up using insertRowAtIndexPath rather that reloading the table. 如此高效,我最终使用了insertRowAtIndexPath而不是重新加载表。 Now it behaves the way it 现在它的行为方式

Thanks so much! 非常感谢! Joe

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

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