Nischal Hada
Nischal Hada

Reputation: 3288

correctly use aspect ratio aspect fill in iOS Xcode

Aspect ratio "Scale To Fill" and "Aspect Fit" make the image stretched to whole UIImageView size. How can I use Aspect Fill" to handell UIImageView inside UITableViewCell. UIImageView inside UITableViewCell is running out of the screen in Aspect Fill. I want to make the image to take is original size rather then to stretch but I want to prevent it form overlapping over its lower cell. Rather its better if it goes below the lower cell.

First Scale To Fill Second Aspect Fill

enter image description here I want to use Aspect Fill and gain as below:

enter image description here

I use following to select the image mode

enter image description here

My code

Cell.m

- (void)awakeFromNib {
     _img.clipsToBounds = YES;
    _img.contentMode = UIViewContentModeScaleAspectFill;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
  }

VCUIViewController.m

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        static NSString *CellIdentifier = @"NoticiasDetailImageCell";
        NoticiasDetailImageCell *cell = (NoticiasDetailImageCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil];
            cell = (NoticiasDetailImageCell *)[nib objectAtIndex:0];
        }
        cell.img.image = self.img;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;

    }

Upvotes: 1

Views: 2407

Answers (1)

Gurtej Singh
Gurtej Singh

Reputation: 3234

You should try to use:

image.clipsToBounds = YES;
image.contentMode = UIViewContentModeScaleAspectFill;

Does it give you the desired results?

Upvotes: 6

Related Questions