在uitableview延迟加载中设置什么条件来加载图像

时间:2013-03-07 09:37:46

标签: iphone uitableview

我从json中获取图像并将所有图像url放在数组中,而不是再次调用json。现在我不知道如何设置它在滚动表时加载图像的条件。这是我要求的方法。

+ (NSMutableArray *) createImg: (NSArray*)sampleData
{
NSMutableArray *arrImg = [[NSMutableArray alloc]init];
for(int i=1; i<=[sampleData count]; i++)
{
NSString *strOfUrl = [sampleData objectAtIndex:i];
UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:strOfUrl]]];
    if(img == NULL)
    {
        NSLog(@"null no image");
    }
    else{
        [arrImg addObject:img];
    }   
}
return arrImg;
}

请指导上述内容,如果不清楚,请随时询问。 提前谢谢。

2 个答案:

答案 0 :(得分:0)

我知道这可能不是你想要的。但是,我建议您使用此AsyncImageView。它将完成延迟加载所需的所有逻辑。此外,它将缓存图像。要调用此API:

ASyncImage *img_EventImag = alloc with frame;
NSURL *url = yourPhotoPath;
[img_EventImage loadImageFromURL:photoPath];
[self.view addSubView:img_EventImage];  // In your case you'll add in your TableViewCell.

与使用UIImageView相同。简单,它可以为您完成大部分工作。 AsyncImageView包含UIImageView上的一个简单类别,用于在iOS上异步加载和显示图像,以便它们不会锁定UI,以及用于更高级功能的UIImageView子类。 AsyncImageView适用于URL,因此可以与本地或远程文件一起使用。

加载/下载的图像缓存在内存中,并在发生内存警告时自动清除。 AsyncImageView独立于UIImage缓存运行,但默认情况下,位于应用程序包根目录中的任何图像都将存储在UIImage缓存中,从而避免重复缓存图像。

该库还可以独立于UIImageView加载和缓存图像,因为它提供了对底层加载和缓存类的直接访问。

答案 1 :(得分:0)

使用它。这是滚动时显示图像的完美方式: https://github.com/enormego/EGOImageLoading

相关问题