Search code examples
iosobjective-cuitableviewimageviewsdwebimage

SDWebImage cell image view size wrong on first load


On first load of the table cell image, the image is "squished":

enter image description here

If I hit the back button, and then go forward it refreshes and looks fine:

enter image description here

Any idea why this is happening? Does it have to do with my placeholder image size or something somehow? I just can't think of what it could be.

View Controller:

- (NSURL *)image {
    NSString *string = [NSString stringWithFormat:@"%@", self.details[@"image"]];
    NSURL *url = [NSURL URLWithString:string];
    return url;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    // Configure the cell...
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    }

    switch (indexPath.row) {
        case 0:
            cell.textLabel.text = [self name];
            cell.detailTextLabel.text = @"Name";
            break;
        case 1:
            cell.textLabel.text = [self email];
            cell.detailTextLabel.text = @"Email";
            break;
        case 2:
            cell.textLabel.text = [self phone];
            cell.detailTextLabel.text = @"Phone";
            break;
        case 3:
            cell.textLabel.text = @"Img";
            break;
        default:
            break;
    }
    [cell.imageView sd_setImageWithURL:[self image]
                      placeholderImage:[UIImage imageNamed:@"placeholder.png"]
                             completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                                 // ... completion code here ...
                                 NSLog(@"Loaded: %@", image);
                             }];

    return cell;
}

Solution

  • Try this:

    cell.imageView.contentMode = UIViewContentModeScaleAspectFit;