Search code examples
iosobjective-cuitableviewafnetworking

Image resizes after async download AFNetworking UiTableView


i have implemented AFNetworking to async download the images to my table, everything works fine, but when i scroll down the table the image in the cell shrinks.

my code look like this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    NSDictionary *dictionary = [self.json objectAtIndex:indexPath.row];
    cell.textLabel.text=[dictionary objectForKey:@"name"];
    cell.detailTextLabel.text = [dictionary objectForKey:@"description"];

    NSString *path = [[[[dictionary objectForKey:@"images"] objectAtIndex:0] valueForKey:@"thumb"] valueForKey:@"url"];
    NSString *ruta = [NSString stringWithFormat:@"http://18.221.78.126/ecoferia%@",path];
    NSURL *url = [NSURL URLWithString:ruta];
    [cell.imageView setImageWithURL:url
                      placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
    return cell;
}

here are some screenshots:

picture 1

picture 2

any tips will be vary apreciate. regards.


Solution

  • There are different type of content mode available for image view in ios

    For Example,

    1) The option to scale the content to fit the size of the view by maintaining the aspect ratio. Any remaining area of the view’s bounds is transparent.

    cell.imageView.contentMode = UIViewContentModeScaleAspectFit;
    

    2) The option to scale the content to fill the size of the view. Some portion of the content may be clipped to fill the view’s bounds.

    cell.imageView.contentMode = UIViewContentModeScaleAspectFill;
    

    3) The option to scale the content to fit the size of itself by changing the aspect ratio of the content if necessary.

    cell.imageView.contentMode = UIViewContentModeScaleToFill;
    

    For further details you can refer, Apple Documentation

    • In the case of your comment you can also create Custom UItableview cell or Prototype cell And in that cell's UI you can add imageview and label for title and set the constraint for the image view as per you requirement