2016-05-30 88 views
0
NSURL *url =[NSURL URLWithString: 
@"http://api.kivaws.org/v1/loans/search.json?status=fundraising"]; 

连载如下存储图像到缓存,以避免从URL中的tableview频繁加载

(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]; 
    } 
} 

做这既有图像和数据。我需要将图像存储到缓存中。

+0

用户SDWebimage或AFN负载图像异步 –

+0

这是一个请求API,你需要初始发送请求到服务器,并需要serlize响应后,遵循nitin gohel评论 –

+0

我已经做了序列化,我已经将它加载到tableview中。但事情每次都需要时间从url加载。因为我是新鲜的,我不知道SDWebimage/AFN – Muthukumar

回答

0

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

请遵循该教程,其中包含如何使用简单的一行代码在UIImageView中缓存图像,以及预取所有背景图像的类,只需创建一个包含要预取所有图像的NSArray。

希望有帮助!