我正在根据延迟加载加载UITableView
单元格图像。这是我在indexpath
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;
}
这种从这些网址下载图片的方法。
- (void)downloadImage:(NSIndexPath *)path {
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
格式。这有什么问题。请帮助我。
由于
答案 0 :(得分:1)
问题是您同步加载图像。当您的代码尝试在下载图像之前将“img”对象设置为字典。这使得nil对象被设置为不允许的字典。所以我的想法是在不同的线程上工作。您可以使用异步调用来下载映像。您可以使用一些框架。例如AFNetworking,ASIHTTPRequest(虽然被遗弃)。 您还可以通过perfromSelector方法运行代码以便在不同的线程上下载。
答案 1 :(得分:0)
答案 2 :(得分: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;
}