如何在适合方面的表格视图单元格中调整 uiimageview 的大小



我想将UIImageView调整为aspectfitmode的确切图像大小,而无需 aspectfit 模式附带的额外空间。

另外,我该如何进行此大小调整,以便在UITableViewCells中显示图像时不会出现故障?

您应该将 UIImageView 的高度约束作为 IBOutLet 属性,然后计算 UIImage 的纵横比缩放高度。基于此设置约束常量。

float ratio = SCREEN_WIDTH / width;
float scaledHeight = height * ratio;
//float scaledWidth = width * ratio; uncomment if required
//Should not exceed the Screen size to make the image visible at a time without scrolling.
if (scaledHeight > SCREEN_HEIGHT) {
scaledHeight = SCREEN_HEIGHT;
}
cell.imageHeightConstraint.constant = scaledHeight;
UIImage img = [UIImage imageNamed:@"ABC.jpg"];
[imageView setImage:img];
imageView.frame = CGRectMake(imageView.frame.origin.x, imageView.frame.origin.y,
                             img.size.width, img.size.height);

最新更新