Apple延迟加载导航控制器中的图像,还原为占位符图像

时间:2011-09-01 20:01:49

标签: iphone ios cocoa-touch

我实现了Apple的LazyTableImages项目(link),但在我的版本中,我使用RestKit获取数据,我的UItableviewcontroller被推送到导航堆栈。

所以我避开苹果在app委托中做的任何事情来获取xml。我不认为这是问题所在。我的问题是当你使用导航后退按钮退出UITableviewcontroller或访问另一个tabbar项并返回时,之前加载的图像会显示出来,但会立即加载占位符图像。基本上,恰恰相反。

就像UITableview缓存数据一样,所以当你回来时它会干扰懒惰表图像。我需要知道有没有人实施过这个代码,他们不得不退出?

编辑:

看起来imageDownloader第二次不是nil,这会阻止图像加载。我还在弄清楚如何绕过它。当然,我可以解决这个问题,但我不知道这对于表现是否“糟糕”。

imageDownloadsInProgress,一个可变字典,即使你退出也仍然拥有它的所有数据。现在已成为一个不同的问题,如果用户回击或偏离当前视图,如何删除imageDownloadsInProgress。

imageDownloadsInProgress被保留,但我在dealloc方法中添加了[imagesDownloadsInProgress release],但是我认为不会运行。

-(void)startEventImageDownload:(WhatsonEvent *)eventRecord forIndexPath:(NSIndexPath *)indexPath
{     

    EventImageDownloader *imageDownloader = [imageDownloadsInProgress objectForKey:indexPath];
    if(imageDownloader == nil)
    {
        NSLog(@"%@",eventRecord.title);
        imageDownloader = [[EventImageDownloader alloc] init];
        imageDownloader.eventRecord = eventRecord;
        imageDownloader.indexPathInTableView = indexPath;
        imageDownloader.delegate = self;
        [imageDownloadsInProgress setObject:imageDownloader forKey:indexPath];
        [imageDownloader startDownload];
        [imageDownloader release];

        [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

    }
}

2 个答案:

答案 0 :(得分:1)

我这样做的方法是构建自己的缓存并将图像保存在用户文档目录中。当我打电话给[tableView reloadData]时(你正在呼叫那个,对吗?),如果图像在本地,它首先会检查每个单元格,否则会懒惰地从源代码中加载它们。如果您需要此代码,请告诉我。

答案 1 :(得分:0)

问题是self.imageDownloadsInProgess = [NSMutableDictionary字典]被放置在ViewDidLoad方法中,目的是每次都重置字典。但是,如果将代码放在推入导航控制器的视图中,则ViewDidLoad仅在第一次执行(我不是肯定的情况)。我将该行添加到ViewWillAppear,因为每次将视图放置在屏幕上时它都会运行。