2014-02-13 88 views
2

我有一個自定義類,解析通過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字符串的圖像?

任何意見,將不勝感激,謝謝。

+1

使用AFNetWorking到Asyncronus載入圖像 –

+0

[cell.imageView setImageWithURL:[NSURL URLWithString:書valueForKey:@ 「照片」]] placeholderImage:無]。 –

+0

請給出正確的答案,正確的代碼是 – Rajjjjjj

回答

2

使用此加載ImageView的

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


    //get a dispatch queue 
    dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); 
//this will start the image loading in bg 
dispatch_async(concurrentQueue, ^{ 
     NSURL *imageURL = [NSURL URLWithString:[[self.listOfPlaceDetails objectAtIndex:indexPath.row]objectForKey:@"imageCell"]]; 

    NSData *image = [[NSData alloc] initWithContentsOfURL:imageURL]; 

    //this will set the image when loading is finished 
    dispatch_async(dispatch_get_main_queue(), ^{ 
     cell.imageView.image = [UIImage imageWithData:image]; 
    }); 
     }); 

    return cell; 
    } 
+0

謝謝Rajjjjj,它可以工作,但由於某些原因,表格在用戶開始滾動或點擊它時纔會顯示任何內容,第二個問題是 - 單元格顯示錯誤的圖像(它一次又一次地顯示錯誤的圖像),只顯示正確的一個.. –

2

下載該項目,並在你的細胞的imageview的使用asynimageveiw .. link

3

嘗試加載圖像異步,所以主線程不會阻塞。

您的代碼將如下所示:

- (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"]]; 
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
     NSData *imageData = [NSData dataWithContentsOfURL:imageURL]; 
     UIImage *image = [UIImage imageWithData:imageData]; 

     dispatch_async(dispatch_get_main_queue(), ^{ 
      cell.imageView.image = image; 
     });  
    }); 

    return cell; 
} 
+1

問題。它會在每個單元格加載時從url加載圖像。但根據問題的答案很好。 – Pawan