从NSDocumentDirectory延迟加载UITableViewCells的图像? [英] Lazy load images for UITableViewCells from NSDocumentDirectory?

查看:110
本文介绍了从NSDocumentDirectory延迟加载UITableViewCells的图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中有一个 UITableView ,我从 NSDocumentDirectory 中加载了几个图像。它工作,但是当向上和向下滚动时,应用程序似乎冻结了一点,很可能是因为主线程中提供了图像,有效地阻止了tableView滚动直到它被加载。我的问题是我不知道如何在以后加载它们,一个延迟加载功能,同时滚动。

I have a UITableView in my app in which I load several images from the NSDocumentDirectory. It works, but when scrolling up and down, the app seems to freeze a bit, most likely because of the images being served in the main thread, effectively blocking the tableView from scrolling until it's loaded. My problem is that I don't know how to load them in later, a "lazy load" feature, while scrolling.

这是用于加载的代码片段图像现在:

This is the code snippet used to load the images now:

imagesPath = [NSString stringWithFormat:@"%@/images/", [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]];
if ([fileManager fileExistsAtPath:[imagesPath stringByAppendingPathComponent:[NSString stringWithFormat:@"/%d.png", rowID]]]) {
    UIImage *image = [[UIImage alloc] initWithContentsOfFile:[imagesPath stringByAppendingPathComponent:[NSString stringWithFormat:@"/%d.png", rowID]]];
    // If image contains anything, set cellImage to image. If image is empty, use default, noimage.png.
    if (image != nil){
        // If image != nil, set cellImage to that image
        cell.cellImage.image = image;
    }
    [image release];
}

将每个单元格中的图像延迟加载的最佳方法是什么避免在滚动中滞后?

What is the best way to "lazy load" the images in each cell to avoid lagging in the scrolling?

推荐答案

看看 SDWebImage 存储库。它提供了执行异步图像加载的所有功能。

Take a look at the SDWebImage repository. It provides everything to perform asynchronous image loading.

更新

我刚注意到README中存在一些拼写错误,因此下载本地文件可能无法正常工作。

I just noticed that there is some typos in the README, so downloading a local file may not work as expected.

以下是一些示例代码。视图控制器有一个UIImageView插座,想要加载 image.jpg 文件。它实现了 SDWebImageManagerDelegate 协议:

Here is some sample code. The view controller has an UIImageView outlet and wants to load the image.jpg file. It implements the SDWebImageManagerDelegate protocol:

- (IBAction)loadImage:(id)sender {
    SDWebImageManager *manager = [SDWebImageManager sharedManager];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *destPath = [documentsDirectory stringByAppendingPathComponent:@"image.jpg"];
    NSURL *url = [NSURL fileURLWithPath:destPath];
    UIImage *cachedImage = [manager imageWithURL:url];
    if (cachedImage)
    {
        imageView.image = cachedImage;
    }
    else
    {
        // Start an async download
        [manager downloadWithURL:url delegate:self];
    }    
}

- (void)webImageManager:(SDWebImageManager *)imageManager didFinishWithImage:(UIImage *)image
{
    imageView.image = image;
}

这篇关于从NSDocumentDirectory延迟加载UITableViewCells的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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