我有一個自定義類,解析通過XML並獲取圖像(我存儲在一個數組中)的URL字符串。然後我想要檢索這些字符串來加載圖像並將其顯示在UITableViewCell中。這裏是我的代碼:從URL加載圖像爲UITableViewCell(需要異步加載)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
UILabel *labelText = (UILabel *)[cell viewWithTag:1000];
labelText.text = [[self.listOfPlaceDetails objectAtIndex:indexPath.row] objectForKey:@"name"];
NSURL *imageURL = [NSURL URLWithString:[[self.listOfPlaceDetails objectAtIndex:indexPath.row]objectForKey:@"imageCell"]];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage *image = [UIImage imageWithData:imageData];
cell.imageView.image = image;
return cell;
}
顯然,當我向下滾動表時,應用程序加載更多的圖像,並在圖像加載,用戶無法做任何事情。它看起來像應用程序凍結。我想加載的圖像不是很大(大約8kb)。也許有一種方法可以加載並將它們存儲在背景中?我也想給用戶更多的自由,比如向下滾動並用文本查看單元格,但沒有圖像。然後強制應用程序加載用戶當前正在查看的單元格的圖像。
有沒有辦法修改我的代碼或任何其他解決方案,以正確加載來自URL字符串的圖像?
任何意見,將不勝感激,謝謝。
使用AFNetWorking到Asyncronus載入圖像 –
[cell.imageView setImageWithURL:[NSURL URLWithString:書valueForKey:@ 「照片」]] placeholderImage:無]。 –
請給出正確的答案,正確的代碼是 – Rajjjjjj