正确使用纵横比 aspect 填写 iOS Xcode

correctly use aspect ratio aspect fill in iOS Xcode

长宽比 "Scale To Fill""Aspect Fit" 使图像拉伸到整个 UIImageView 大小。如何使用 Aspect Fill" 处理 UITableViewCell 中的 UIImageView。UITableViewCell 中的 UIImageView 运行 在 Aspect Fill 屏幕外。我想将图像设为 采用原始尺寸而不是拉伸,但我想防止它在其较低的单元格上形成重叠。如果它低于较低的单元格则更好。

第一个缩放填充第二个纵横比填充

我想使用 Aspect Fill 并获得如下结果:

我使用以下select图像模式

我的代码

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;

    }

您应该尝试使用:

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

它给了你想要的结果吗?