自定义UIImageView中的SDWebImage UIImageView + WebCache

时间:2013-10-31 09:04:18

标签: ios uitableview uiimageview sigabrt sdwebimage

我在将我的网络服务器中的图像加载到桌面视图中的UIImageView时出现问题

这段代码很完美,但是我的桌面视图中的图像很奇怪和丑陋:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //Get the cell. Note that this name is the same as in the storyboard
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    //Set the correct name in the cell.
    //Do so by looking up the row in indexpath and choosing the same element in the array
    NSInteger currentRow = indexPath.row;
    Image* currentImage = [self.images objectAtIndex:currentRow];

    // Here we use the new provided setImageWithURL: method to load the web image
    [cell.imageView setImageWithURL:[NSURL URLWithString:currentImage.src] placeholderImage:[UIImage imageNamed:@"testimage.jpg"]];
    return cell;
}

所以我在故事板上创建了一个UIImageView,并将代码更改为:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //Get the cell. Note that this name is the same as in the storyboard
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    //Set the correct name in the cell.
    //Do so by looking up the row in indexpath and choosing the same element in the array
    NSInteger currentRow = indexPath.row;
    Image* currentImage = [self.images objectAtIndex:currentRow];

    UIImageView *imgView = (UIImageView *)[cell viewWithTag:100];
    // Here we use the new provided setImageWithURL: method to load the web image
    [imgView setImageWithURL:[NSURL URLWithString:currentImage.src] placeholderImage:[UIImage imageNamed:@"testimage.jpg"]];
    return cell;
}

但如果我在我的设备上运行此代码,我收到SIGABRT信号错误。

如何在我的自定义Imageview上使用它?

2 个答案:

答案 0 :(得分:0)

在第一个代码段中,添加以下行:

cell.imageView.contentMode = UIViewContentModeScaleAspectFill;
cell.imageView.clipsToBounds = YES;

答案 1 :(得分:0)

您正在获取SIGABRT,因为您的imagevIview未添加到单元格,而是单元格的contentView ..!因此,获取imageView的正确方法应该是,

UIImageView *imgView = (UIImageView *)[cell.contentView viewWithTag:100];