我最近有一個類似的問題,但並不需要優先加載。我想不出如何改變加載內容的優先級,除非你在加載tableview之前加載縮略圖。根據需要使用表格稱重傳感器。
我能想到的兩個選項:
如果你想把所有的數據加載並準備好實現代碼如下甚至被加載之前,搶先加載在前面的視圖控制器中的數據並將其保存在被打開包含你的表格視圖的視圖控制器。然後,您的tableview不會有任何請求,所有內容都將無縫顯示。
tableview異步發送每個縮略圖請求,並在圖像到達主線程時更新單元格。然後,您可以在圖像到達後緩存或保存圖像。但圖像不會立即出現,通常在一秒之後。
我做了使用NSCache的選項二。關鍵「頭像」的值是縮略圖圖像的URL。此代碼位於我- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
if (![teamMember[@"avatar"] isEqualToString:@""]) {
// check for cached image, use if it exists
UIImage *cachedImage = [self.imageCache objectForKey:teamMember[@"avatar"]];
if (cachedImage) {
cell.memberImage.image = cachedImage;
}
//else retrieve the image from server
else {
NSURL *imageURL = [NSURL URLWithString:teamMember[@"avatar"]];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
// if valid data, create UIImage
if (imageData) {
UIImage *image = [UIImage imageWithData:imageData];
// if valid image, update in tableview asynch
if (image) {
dispatch_async(dispatch_get_main_queue(), ^{
TeamCommitsCell *updateCell = (id)[tableView cellForRowAtIndexPath:indexPath];
// if valid cell, display image and add to cache
if (updateCell) {
updateCell.memberImage.image = image;
[self.imageCache setObject:image forKey:teamMember[@"avatar"]];
}
});
}
}
});
}
}
'SDWebImage'是使用'NSOperation'子類和'NSOperationQueue',所以你可以,假設,提供'SDWebImageDownloader'公共方法更改操作的隊列優先級。但是,我認爲這不會影響已經執行的操作。我相信你必須取消並重新啓動它們。 –