问题描述
下午好,
我第一次尝试在我的 TableViewController 中使用 SDWebImage,我在使用setImageWithURL"属性时遇到了一些问题,因为它只允许 UIImageView 但我有一个 UIImage 以便解析来自我的 JSON 的数据.
Im trying to use SDWebImage in my TableViewController for the first time and I have some problems using the "setImageWithURL" property because it only admits a UIImageView but I have a UIImage in order to parse the data from my JSON.
我必须如何更改代码才能使用 UIImageView 而不是 UIImage?因为 UIImageView 没有与 UIImage 相同的功能,所以我必须从 URL 创建我的图像.
How I have to change my code in order to use a UIImageView instead of a UIImage? Because UIImageView doesn't have the same functions as UIImage and I have to create my Image from a URL.
这是我的代码,希望你能帮我一些提示:
Here is my code, I hope you can help me with some tips:
- (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;
}
非常感谢.
推荐答案
当你使用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;
这篇关于TableViewController 使用 SDWebImage 和 UIImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!