Objective c 是否从NSDocumentDirectory延迟加载UITableViewCell的图像?

Objective c 是否从NSDocumentDirectory延迟加载UITableViewCell的图像?,objective-c,uitableview,ios4,uiimage,Objective C,Uitableview,Ios4,Uiimage,我的应用程序中有一个UITableView,我从NSDocumentDirectory加载了多个图像。它可以工作,但当上下滚动时,应用程序似乎有点冻结,很可能是因为主线程中提供了图像,有效地阻止了tableView在加载之前的滚动。我的问题是,我不知道以后如何在滚动时加载它们,这是一个“延迟加载”功能 这是用于立即加载图像的代码段: imagesPath = [NSString stringWithFormat:@"%@/images/", [NSSearchPathForDirectories

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

这是用于立即加载图像的代码段:

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];
}
“延迟加载”每个单元格中的图像以避免滚动滞后的最佳方法是什么?

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

更新

我只是注意到自述文件中有一些输入错误,因此下载本地文件可能无法按预期工作

下面是一些示例代码。视图控制器有一个UIImageView出口,希望加载
image.jpg
文件。它实现了
SDWebImageManagerDelegate
协议:

- (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加载图像。下载应理解为访问NSURL支持的任何资源。NSURL可用于多种协议:http、ftp或文件。将您的路径转换为NSURL并“下载”您的本地文件。我正在尝试,但它不起作用!NSURL是从字符串加载的:
file://var/mobile/Applications/RANDOMIDENTIFIER/Documents/images/10.png
。谢谢,这可能行得通,但我已经决定使用SDWebImage框架,这样我就不需要下载和存储图像了:)不过,谢谢@Laurent Etiemble:我正在传递我的图像路径,它来自应用程序的文档目录。现在,当我尝试运行该应用程序时,占位符图像会出现,但即使经过很长时间,真实图像也不会加载