iphone tableview有很多图像 [英] iphone tableview with a lot of images

查看:170
本文介绍了iphone tableview有很多图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UITableView ,它在单元格中显示了很多图像,我对滚动性能并不满意。我的 UITableView 类似于iphone上的照片应用程序。有没有人知道为什么iphone的照片应用程序滚动如此快,好像他们的屏幕上没有任何内容。

I have a UITableView that displays a lot of images in the cells, and i am not quit happy about the scroll performance. My UITableView is something like the photo app on the iphone. Does anybody knows why the iphone foto app scrolls so damn fast as if their's nothing on the screen.

并且有人提供一些提示/技巧来提高我的性能/滚动速度?

And does anybody has some tips/tricks to increase my performance/scrolling speed?

推荐答案

你应该预先处理你的图像,而不是懒散地做。当您滚动表格时, UITableViewDataSource:cellForRowAtIndexPath:方法被调用,如果您在那里加载图像,那么您将看到它请求您的单元格内容作为滚动,从而在您的应用程序中创建延迟。尝试制作你的cellForRowAtIndexPath:类似这样:

You should precache your images and not do it lazily. As you scroll your table the UITableViewDataSource:cellForRowAtIndexPath: method is called, and if you're loading your images in there then you'll see it requesting your cell contents as your scroll, creating latency in your application. Try putting making your cellForRowAtIndexPath: something like this:

NSDate *date = [NSDate date];
... your cell loading code ...
NSLog( @"Elapsed time to generate cell %.2d", [date timeIntervalSinceNow] );

您将看到花费多长时间来获取每个单元格。

You'll see how long you're spending for getting each cell.

要解决这个问题,你可以尽可能地复杂 - 如果你有一个很多的图像,你将不得不越来越多聪明。您可以执行分页加载,在其中跟踪请求的最后一个单元格的NSIndexPath,并确定滚动是上升还是下降并使用 + NSImage:imageNamed:立即获取一些页面的图像向前(即,当前位置前方的5个图像),或者任何适合您的图像(利用人们必须将手指向下移动到桌子底部以再次滑动的事实,以及表格元素的消耗暂停 - 您可以使页面大小足以填充滑动)。然而,这可能仍然不是很好,因为你只会立即遭受所有影响而不是每个细胞的紧张负荷。

To get around this, you can be as complex as you need to - if you have a lot of images, you're going to have to be more and more clever. You can do paged loading, where you keep track of the last cell requested's NSIndexPath, and determine if the scroll is going up or down and use +NSImage:imageNamed: to fetch at once some page worth of images forward (ie, 5 images forward of your current position), or whatever works out for you (taking advantage of the fact that people have to return their finger down to the bottom of the table to swipe again, and so the consumption of table elements has pauses - you can make your page size large enough to fill a swipe). This is probably still not great, though, because you'll just be suffering all your impact at once instead of a jittery load for every cell.

你可以将控制权归还给UI快速并允许系统使用 NSRunLoop:performSelector:target:argument:order:mode: off the main runloop使用NSImage:imageNamed:,然后当请求单元格时,如果你向前取得足够远,它就可以显示。

You can return control to the UI rapidly and allow the system to schedule your prefetched page of images using NSRunLoop:performSelector:target:argument:order:mode: off the main runloop using NSImage:imageNamed:, and then when the cell is requested if you're fetching far enough ahead it'll be available to display.

你需要痛苦地意识到但是记忆问题。如果您发现这是一个问题,请使用 NSImage:initWithContentsOfFile:,它将清除低内存情况下的图像缓存。根据缓存失效算法使用的策略,这些情况可能会在您清除缓存时导致口吃,并且必须重新加载无效的预取。

You need to be painfully aware of memory concerns though. If you're finding this to be an issue, use NSImage:initWithContentsOfFile:, which will clean up image caches in low memory situations. Depending on the strategy used by the cache invalidation algorithm these situations may cause a "stutter" as you purge caches and have to reload your invalidated prefetches.

这篇关于iphone tableview有很多图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆