简体   繁体   中英

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

在此处输入图片说明 I want to use Aspect Fill and gain as below:

在此处输入图片说明

I use following to select the image mode

在此处输入图片说明

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;

    }

You should try to use:

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

Does it give you the desired results?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM