-1

遅延読み込みUITableViewに従ってセル画像を読み込んでいます。これがインデックスパスの行のセルのコードです

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 から画像をダウンロードするためのこのメソッド。

-(void)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

しかし、ブラウザからそのリンクにアクセスすると、画像が正しく表示されます。これらの画像はすべて.jpgフォーマットです。これの何が問題なのですか。助けてください。

ありがとう

4

3 に答える 3

1

問題は、画像の同期読み込みです。画像がダウンロードされる前に、コードが「img」オブジェクトを辞書に設定しようとするため。これにより、許可されていないディクショナリに設定される nil オブジェクトが作成されます。したがって、アイデアは、さまざまなスレッドで作業することです。非同期呼び出しを使用してイメージをダウンロードできます。使用できるフレームワークがいくつかあります。例: AFNetworking、ASIHTTPRequest (放棄されていますが)。perfromSelector メソッドを使用して、別のスレッドでダウンロードするコードを実行することもできます。

于 2013-05-28T06:12:53.633 に答える
0

完璧なライブラリSDWebImageを使用する必要があります

非同期でダウンロードするだけでなく、そのような致命的なエラーを処理して、アプリケーションのクラッシュを防ぎます。指定された URL で画像が見つからない場合は、プレースホルダー画像も提供します。実装は非常に簡単です。

- (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;
}
于 2013-05-28T06:17:47.527 に答える
0

あなたは良い考えではない同期イメージダウンロードを使用しています

非同期イメージ ダウンロードを使用する必要があります

これをチェックして

iOS - 非同期イメージ ダウンロード

于 2013-05-28T05:57:46.133 に答える