目前,我正在爲網站的客戶端工作。我有一噸圖像,我需要加載到我的TableView。以下是我在做currectly:緩存NSData(與UIImages)
NSDictionary *propertyItems = [self.items objectAtIndex:indexPath.row];
cell.fio.font = [UIFont boldSystemFontOfSize:17.0f];
if([propertyItems objectForKey:@"online"] == [NSNumber numberWithInt:1])
cell.fio.textColor = [UIColor colorWithRed:0.3 green:0.6 blue:0.3 alpha:1.0];
dispatch_queue_t downloadPhotosQueue = dispatch_queue_create("downloadPhotosQeue", NULL);
dispatch_async(downloadPhotosQueue, ^{
NSData *photosData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.example.com/%@", [propertyItems objectForKey:@"photo_trumb"]]]];
dispatch_async(dispatch_get_main_queue(), ^{
cell.pic.image = [UIImage imageWithData:photosData];
});
});
cell.fio.text = [propertyItems objectForKey:@"fio"];
我這樣做是在的cellForRowAtIndexPath:方法。 一切正在加載更快。但問題是,當我將桌子向下滾動時,再次上來後,圖像一遍又一遍地重新加載。
問題:有沒有什麼辦法可以輕鬆緩存我從服務器上獲取的UIImages?所以如果他們被加載一次,他們不會一遍又一遍地重新加載,而我正在運行應用程序。也許我做錯了什麼?
在此先感謝!