2013-05-28 30 views
-1

我正在根據延遲加載加載UITableView單元格圖像。這裏是我的代碼爲索引路徑行的單元格由於未捕獲異常'NSInvalidArgumentException'導致終止應用程序崩潰,原因:' - [__ NSCFDictionary setObject:for

else if (tableView.tag==4)//All Songs 
{ 

    cell4=(SongCell *)[tableView dequeueReusableCellWithIdentifier:@"SongCell"]; 



    if (cell4 == nil) { 

     NSArray *nib =[[NSBundle mainBundle]loadNibNamed:@"SongCell" owner:self options:nil]; 
     cell4=[nib objectAtIndex:0]; 

    } 




    [cell4 setSelectionStyle:UITableViewCellSelectionStyleNone]; 

    cell4.lblSong.text=[[arrSongList objectAtIndex:indexPath.row] valueForKey:@"SONGTITLE"]; 
    cell4.lblArtist.text=[[arrSongList objectAtIndex:indexPath.row] valueForKey:@"ARTISTNAME"]; 


    if ([dicSongimages valueForKey:[[arrSongList objectAtIndex:indexPath.row] valueForKey:@"SONGIMG"]]) { 
     cell4.imgSong.image=[dicSongimages valueForKey:[[arrSongList objectAtIndex:indexPath.row] valueForKey:@"SONGIMG"]]; 

     [cell4.activityIndicator stopAnimating]; 
     cell4.activityIndicator.hidden=YES; 

    } 





     else 
     { 
      if (!isDragging_msg && !isDecliring_msg) 
      { 
       [dicSongimages setObject:[UIImage imageNamed:@"placeholder.png"] forKey:[[arrSongList objectAtIndex:indexPath.row] valueForKey:@"SONGIMG"]]; 
       cell4.activityIndicator.hidden=NO; 
       [cell4.activityIndicator startAnimating]; 

       [self performSelectorInBackground:@selector(downloadImage:) withObject:indexPath]; 
      } 
      else 
      { 
       cell4.imgSong.image=[UIImage imageNamed:@"placeholder.png"]; 
       cell4.activityIndicator.hidden=YES; 
       [cell4.activityIndicator stopAnimating]; 
      } 

     } 





    [cell4.btnTick addTarget:self action:@selector(tickButtonTapped:) forControlEvents:UIControlEventTouchUpInside] ; 

    [cell4.btnAddtoMyList addTarget:self action:@selector(topLeft:) forControlEvents:UIControlEventTouchUpInside]; 
    return cell4; 
} 

此方法從這些URL下載圖像。

- (空)downloadImage:(NSIndexPath *)路徑{

if(path.row<[arrSongList count]) 
{ 

NSString *str=[[arrSongList objectAtIndex:path.row]valueForKey:@"SONGIMG"]; 

UIImage *img = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:str]]]; 




[dicSongimages setObject:img forKey:str]; 

[tblSongs performSelectorOnMainThread:@selector(reloadData) withObject:@"TAG_4" waitUntilDone:NO]; 

}

我的問題是

[dicSongimages setObject:img forKey:str];越來越崩潰,它說

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary setObject:forKey:]: attempt to insert nil value

但圖像正確顯示w如果我通過瀏覽器訪問該鏈接。所有這些圖像都是.jpg格式。這有什麼問題。 PLZ幫助我。

感謝

+0

檢查一次placeholder.png圖像有或沒有? – Balu

+0

是的,它在那裏 – iDia

+0

只有當路徑是[0,47] – iDia

回答

1

的問題是你的同步加載圖像。當您的代碼嘗試在圖像下載之前將'img'對象設置爲字典。這使得一個零對象被設置到一個不允許的字典中。所以這個想法是在不同的線程上工作。您可以使用異步調用來下載圖像。有些框架可供您使用。例如AFNetworking,ASIHTTPRequest(儘管被放棄)。 您也可以通過perfromSelector方法運行代碼以在不同線程上下載。

0

你應該使用異步完美庫SDWebImage

它不僅下載,而且處理這些致命錯誤,防止應用程序崩潰。如果在指定的網址找不到圖片,也提供佔位符圖片。 很容易實現。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *MyIdentifier = @"MyIdentifier"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 

    if (cell == nil) 
    { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
             reuseIdentifier:MyIdentifier] autorelease]; 
    } 

    // Here we use the new provided setImageWithURL: method to load the web image 
    [cell.imageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"] 
        placeholderImage:[UIImage imageNamed:@"placeholder.png"]]; 

    cell.textLabel.text = @"My Text"; 
    return cell; 
} 
+0

它崩潰,我們可以在這個代碼中使用這個庫? – iDia

+0

您可以使用它填充tableview行,甚至可以在任何想要顯示來自URL的圖像的地方使用它。它還緩存圖像,所以如果請求再次發送到相同的URL,它將顯示來自緩存的圖像,而不會再次下載,這會緊縮應用程序UI。 – Khawar

+0

即使Facebook使用它根據庫作者https://github.com/rs/SDWebImage/wiki/Who-Use-SDWebImage – Khawar

相關問題