将图像存储到缓存中,以避免在 tableview 中从 url 频繁加载

Store the image into cache for avoiding frequent loading from url in tableview

NSURL *url =[NSURL URLWithString:
@"http://api.kivaws.org/v1/loans/search.json?status=fundraising"];

序列化部分在下面完成

(void)connectionDidFinishLoading{
    NSDictionary *allDict = [NSJSONSerialization JSONObjectWithData:webData options:0 error:nil];
    NSArray *loans = [allDict objectForKey:@"loans"];
    countryArray = [[NSMutableArray alloc] init];
    for (NSDictionary *diction in loans) {
        Country *countryObj = [[Country alloc]init];
        NSString *sector =  [diction objectForKey:@"sector"];
        countryObj.sector = sector;
        NSDictionary *imageID = [diction objectForKey:@"image"];
        NSString *img = [imageID objectForKey:@"id"];
        countryObj.idValue=img;
        NSString *activity =  [diction objectForKey:@"activity"];
        countryObj.activity = activity;
                NSString *name =  [diction objectForKey:@"name"];
        countryObj.name = name;
        NSDictionary *con = [diction objectForKey:@"location"];
        NSString *world = [con objectForKey:@"country"];
        countryObj.country= world;
        NSString *towname = [con objectForKey:@"town"];
        countryObj.down=towname;
        [countryArray addObject:countryObj];
    }
}

这包含图像和数据。我需要将图像存储到缓存中。

请使用SDWebImage它包含您需要的一切。我已经使用这个库 2 年了。

遵循该教程,其中包含如何使用简单的一行代码在 UIImageView 中缓存图像,还有一个 class 在后台预取所有图像,只需创建一个包含所有你想要的图像的 NSArray预取。

希望对您有所帮助!