关于 Apple 的 LazyTableImages 示例的问题 - 与应用商店的行为不完全相同 [英] Question about Apple's LazyTableImages sample - Doesn't behave exactly like the app store

查看:15
本文介绍了关于 Apple 的 LazyTableImages 示例的问题 - 与应用商店的行为不完全相同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有项目列表的 UITableView,每个项目都有自己的图像.我认为 Apple 的 LazyTableImages 示例项目非常适合学习,并用于在检索到原始列表数据后实现同一种异步下载图像的过程.

在大多数情况下,它运行良好,只是我注意到此示例应用程序与实际应用商店下载图像的方式之间的行为存在细微差别.

如果您启动 LazyTableImages 示例,然后快速向下滑动,您会看到图像直到滚动完全停止之后才会显示.

现在,如果您对实际应用商店中的项目列表进行相同的测试,您会看到图像在新项目出现时立即开始显示,即使滚动尚未停止.

我正在努力实现这些相同的结果,但到目前为止我没有取得任何进展.有人对如何做到这一点有任何想法吗?

谢谢!

解决方案

我很困惑,没有人能回答这个问题......

所以,关于图标的下载/显示方式,我最终找到了完全相同与实际应用商店中使用的效果相同的效果.

获取 LazyTableImages 示例项目并进行一些简单的修改.

  1. 进入根视图控制器并删除所有关于表格滚动和/或在 cellForRowAtIndexPath 中减速的检查

  2. 删除所有对 loadImagesForOnScreenRows 的调用,从而也删除该方法.

  3. 进入 IconDownload.m 并将 startDownload 方法更改为进行异步图像下载,而是在后台线程上进行同步下载.把startDownload里面的代码全部去掉,加上下面的代码,就变成这样了:

<上一页><代码>- (void)startDownload{NSOperationQueue *queue = [NSOperationQueue 新];NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self 选择器:@selector(loadImage) object:nil];[队列添加操作:操作];【操作发布】;[队列释放];}

然后,添加一个 loadImage,如下所示:

<上一页><代码>- (void)loadImage{NSData *imageData = [[NSData alloc] initWithContents OfURL:[NSURL URLWithString:appRecord.imageURLString]];self.apprecord.appIcon = [UIImage imageWithData:imageData];【图片数据发布】;[self performSelectorOnMainThread:@selector(notifyMainThread) withObject:nil waitUntilDone:NO];}

然后,像这样添加 notifyMainThread:

<上一页><代码>- (void)notifyMainThread{[委托 appImageDidLoad:self.indexPathInTableView];}

完成!运行它,您将看到确切的应用商店行为,不再等待请求图像下载直到滚动停止,也不再等待图像显示直到滚动停止,或者直到用户将手指从屏幕上移开.

图像在单元格准备好显示后立即下载,图像在下载后立即显示,句号.

抱歉有任何错别字,我没有从我的应用程序中粘贴这个,我输入了它,因为我现在不在我的 mac...

无论如何,我希望这对大家有帮助......

I have a UITableView with a list of items, each having it's own image. I thought Apple's LazyTableImages sample project would be perfect to learn from, and use to implement the same kind of process of downloading images asynchronously, after the original list data is retrieved.

For the most part, it works quite well, except I did notice a subtle difference in behavior, between this sample app, and how the actual app store downloads images.

If you launch the LazyTableImages sample, then do a quick flick-scroll down, you'll see that the images do not get displayed until after the scrolling comes to a complete stop.

Now, if you do the same test with a list of items in the actual app store, you'll see that the images start displaying as soon as the new items come into view, even if scrolling hasn't stopped yet.

I'm trying to achieve these same results, but so far I'm not making any progress. Does anyone have any ideas on how to do this?

Thanks!

解决方案

I'm baffled that nobody could answer this...

So, I eventually figured out how to acheive the exact same effect that is used in the actual app store, in regards to how the icons are downloaded/displayed.

Take the LazyTableImages sample project and make a few simpled modifications.

  1. Go into the root view controller and remove all checks regarding is table scrolling and/or decelerating in cellForRowAtIndexPath

  2. Remove all calls to loadImagesForOnScreenRows, and thus remove that method as well.

  3. Go into IconDownload.m and change the startDownload method to not do an async image downlaod, but instead do a sync download on a background thread. Remove all the code in startDownload, and add the following, so it looks like this:


- (void)startDownload
{
    NSOperationQueue *queue = [NSOperationQueue new];
    NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(loadImage) object:nil];

    [queue addOperation:operation];

    [operation release];
    [queue release];
}

Then, add a loadImage, like this:


- (void)loadImage
{
    NSData *imageData = [[NSData alloc] initWithContents OfURL:[NSURL URLWithString:appRecord.imageURLString]];
    self.apprecord.appIcon = [UIImage imageWithData:imageData];
    [imageData release];

    [self performSelectorOnMainThread:@selector(notifyMainThread) withObject:nil waitUntilDone:NO];
}

Then, add notifyMainThread like this:


- (void)notifyMainThread
{
    [delegate appImageDidLoad:self.indexPathInTableView];
}

Done! Run it, and you will see the exact app store behavior, no more waiting to request image downloads until scrolling stops, and no more waiting for images to display until scrolling stops, or until user has removed their finger from the screen.

Images are downloaded as soon as the cell is ready to be displayed, and the image is displayed as soon as it is downloaded, period.

Sorry for any typos, I didn't paste this from my app, I typed it in, since I'm away from my mac right now...

Anyway, I hope this helps you all...

这篇关于关于 Apple 的 LazyTableImages 示例的问题 - 与应用商店的行为不完全相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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