如何在UITableViewCell中显示和加载URL中的图像

时间:2017-06-05 07:49:35

标签: ios objective-c uitableview

我尝试了this link,但我没有得到我的解决方案。

我想在UITableViewCell中显示和加载来自URL的图像,当我们在Objective-C中滚动表视图时,必须加载其他单元格图像。

2 个答案:

答案 0 :(得分:0)

使用SDWebImage。它几乎具备您所需的所有功能。这样编码也很容易。

目标C

[imageView sd_setImageWithURL:[NSURL URLWithString:@"your domain"]
             placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

其他

swift 3:

imageView.sd_setImage(with: URL(string: "your domain"), placeholderImage: UIImage(named: "placeholder.png"))

答案 1 :(得分:0)

Rajashekar只是你可以做到这一点

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *strCell=@"cell";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:strCell];
    if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:strCell];
    }
    NSString *strImgURL = @"YourImageURL";
    NSError* error = nil;
    NSURL *fileURL = [NSURL fileURLWithPath:strImgURL];
    NSData* data = [NSData dataWithContentsOfURL:fileURL options:NSDataReadingUncached error:&error];
    if (error) {
        NSLog(@"%@", [error localizedDescription]);
    } else {
        NSLog(@"Data has loaded successfully.");
    }
    UIImage *img = [[UIImage alloc] initWithData:data];
    cell.imageView.image = img;

    return cell;
}