Ios 由于未捕获异常,延迟加载导致应用程序终止时崩溃';NSInvalidArgumentException';,原因:'-[\uu NSCFDictionary setObject:用于
我正在根据延迟加载来加载Ios 由于未捕获异常,延迟加载导致应用程序终止时崩溃';NSInvalidArgumentException';,原因:'-[\uu NSCFDictionary setObject:用于,ios,uitableview,lazy-loading,Ios,Uitableview,Lazy Loading,我正在根据延迟加载来加载UITableView单元格图像 else if (tableView.tag==4)//All Songs { cell4=(SongCell *)[tableView dequeueReusableCellWithIdentifier:@"SongCell"]; if (cell4 == nil) { NSArray *nib =[[NSBundle mainBundle]loadNibNamed:@"SongCell" owne
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)下载图像:(nsindepath*)路径{
if(path.row您使用的是同步图像下载,这不是个好主意
您需要使用异步映像下载
检查这个
问题在于同步加载图像。因为代码试图在图像下载之前将“img”对象设置到字典中。这使得nil对象被设置到字典中,这是不允许的。因此,想法是在不同的线程上工作。可以使用异步调用下载图像。有一些你可以使用的框架,例如AFNetworking,ASIHTTPRequest(尽管被放弃了)。
您还可以通过perfromSelector方法在不同的线程上运行下载代码。您应该使用一个完美的库
它不仅可以异步下载,还可以处理此类致命错误,以防止应用程序崩溃。如果在指定的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;
}
一旦检查placeholder.png图像是否存在?仅当路径为[0,47]时才会崩溃您可以在填充tableview行时使用此选项,甚至在您希望从URL显示图像的任何位置都可以使用此选项。它还缓存图像,因此,如果请求再次发送到同一URL,它将从缓存中显示图像,而无需再次下载,从而固定应用程序UI。根据库作者的说法,即使Facebook也会使用此选项
- (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;
}