我有一个UITableView自定义单元格showng文本和图像。(图像在右侧)。起初,它一切正常,但后来我注意到,当滚动时,整个桌面视图是滞后的。这是因为应用程序可能会在单元格重新使用时进入屏幕时下载单元格图像。我添加了一个NSCache来存储下载的图像,并告诉cellForRowAtIndexPath加载imageName(如果存在),否则,再次从Internet下载。但是,缓存是如此短暂的存储空间,如果我用主页按钮退出我的应用程序并重新进入,那么只有一些图像保留,并且必须再次下载图像。从网络存储图像为UITableView
我想找出最好的方式来存储图像比缓存更长期。我看过一些关于NSDirectory,并在应用程序库中存储,但还没有想通出来呢..
最逻辑的解决方案,我相信,会是做这样的事情:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
/*stuff*/
NSString *imageName = [dictionary objectForKey:@"Image"];
UIImage *image = [--Cache-directory-- objectForKey:imageName]; //Try to get it from cache
if(image) //If image was received from cache:
{
cell.imageView.Image = image;
}
else //If not in cache:
{
image = [--local-directory-- objectForKey:imageName]; //Check some local directory
if(image) //If image received from directory:
{
cell.imageView.Image = image;
// + Also save it to the cache?
}
else //If image was in neither cache or local directory, get it from the website with given URL
{
image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:imageURL]];
//Then save to cache and to file?
}
}
}
图像被快速地改变或切换出来,但不会那么快速以致于我愿意在应用程序中预先实现它们,以便每次添加图像时都必须发布更新。
这对我来说似乎合乎逻辑。我在正确的轨道上?我该如何“调用”本地目录?就像,如果我将图像添加到NSDirectory对象或其他东西,是不是每次都会重置?我如何访问本地文件夹?
好吧,我会试试这个,但是这不会导致每次启动它时tableview都会变空,最终会自动填充?这些图像会被快速更新,而且我不希望在第一次启动应用程序时看到没有图像的单元格。 – Sti 2012-07-17 21:04:45
或者至少,每次您退出该过程时,它似乎都是如此。很不错! – Sti 2012-07-17 21:15:52
有没有办法在图像准备就绪时加载图像,而不是直到单元重新进入屏幕才加载图像?或者我可以在前8行的viewDidLoad中设置图像吗? – Sti 2012-07-17 21:26:14