2016-05-30 97 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。

希望有幫助!