使用带UIImage的SDWebImage的TableViewController

下午好,

我试图在我的TableViewController第一次使用SDWebImage,我有一些问题,使用“setImageWithURL”属性,因为它只承认一个UIImageView,但我有一个UIImage来parsing从我的JSON数据。

我如何改变我的代码,以便使用UIImageView而不是UIImage? 因为UIImageView不具有UIImage相同的function,我必须从URL创build我的图像。

这里是我的代码,我希望你能帮助我一些提示:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"carTableCell"; CarTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[CarTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } // Configure the cell... cell.makeLabel.text = [[_jsonArray objectAtIndex:indexPath.row] valueForKey:@"id"]; cell.likes.text = [[_jsonArray objectAtIndex:indexPath.row] valueForKey:@"likes"]; cell.comments.text = [[_jsonArray objectAtIndex:indexPath.row] valueForKey:@"comments"]; cell.username.text = [[_jsonArray objectAtIndex:indexPath.row] valueForKey:@"username"]; cell.refuser.text = [[_jsonArray objectAtIndex:indexPath.row] valueForKey:@"user_ref"]; cell.modelLabel.text = [[_jsonArray objectAtIndex:indexPath.row] valueForKey:@"user"]; NSURL * imageURL = [NSURL URLWithString:[[_jsonArray objectAtIndex:indexPath.row] valueForKey:@"imagen"]]; NSData * imageData = [NSData dataWithContentsOfURL:imageURL]; UIImage * carPhoto = [UIImage imageWithData:imageData]; cell.carImage.image = carPhoto; /*[cell.imageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];*/ NSURL * imageURL2 = [NSURL URLWithString:[[_jsonArray objectAtIndex:indexPath.row] valueForKey:@"image"]]; NSData * imageData2 = [NSData dataWithContentsOfURL:imageURL2]; UIImage * carPhoto2 = [UIImage imageWithData:imageData2]; cell.profileImage.image = carPhoto2; return cell; } 

非常感激。

改变你的代码为

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"carTableCell"; CarTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[CarTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } // Configure the cell... cell.makeLabel.text = [[_jsonArray objectAtIndex:indexPath.row] valueForKey:@"id"]; cell.likes.text = [[_jsonArray objectAtIndex:indexPath.row] valueForKey:@"likes"]; cell.comments.text = [[_jsonArray objectAtIndex:indexPath.row] valueForKey:@"comments"]; cell.username.text = [[_jsonArray objectAtIndex:indexPath.row] valueForKey:@"username"]; cell.refuser.text = [[_jsonArray objectAtIndex:indexPath.row] valueForKey:@"user_ref"]; cell.modelLabel.text = [[_jsonArray objectAtIndex:indexPath.row] valueForKey:@"user"]; NSURL * imageURL = [NSURL URLWithString:[[_jsonArray objectAtIndex:indexPath.row] valueForKey:@"imagen"]]; [cell.imageView setImageWithURL:imageURL placeholderImage:[UIImage imageNamed:@"placeholder.png"]]; NSURL * imageURL2 = [NSURL URLWithString:[[_jsonArray objectAtIndex:indexPath.row] valueForKey:@"image"]]; [cell.profileImage setImageWithURL:imageURL2 placeholderImage:[UIImage imageNamed:@"placeholder.png"]]; return cell; } 

希望这可以帮助。

当您使用SDWebImage时,您只需添加两行

 [cell.carImage sd_setImageWithURL: [NSURL URLWithString: imageURL]]; [cell.profileImage sd_setImageWithURL: [NSURL URLWithString: imageURL2]]; 

并删除下面的行

 NSURL * imageURL = [NSURL URLWithString:[[_jsonArray objectAtIndex:indexPath.row] valueForKey:@"imagen"]]; NSData * imageData = [NSData dataWithContentsOfURL:imageURL]; UIImage * carPhoto = [UIImage imageWithData:imageData]; cell.carImage.image = carPhoto; /*[cell.imageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];*/ NSURL * imageURL2 = [NSURL URLWithString:[[_jsonArray objectAtIndex:indexPath.row] valueForKey:@"image"]]; NSData * imageData2 = [NSData dataWithContentsOfURL:imageURL2]; UIImage * carPhoto2 = [UIImage imageWithData:imageData2]; cell.profileImage.image = carPhoto2;