简体   繁体   English

UITableViewCell如何在保留宽高比的情况下动态适应新图像?

[英]how can UITableViewCell dynamically adapt to new image with aspect ratio retained?

I'm designing a tableview that can dynamically loads new image into cells. 我正在设计一个可以动态地将新图像加载到单元格中的表视图。

My tableview hierarchy is like this: tableview -> myCustomCell -> content view -> myCustomImageView. 我的表视图层次结构是这样的:表视图-> myCustomCell->内容视图-> myCustomImageView。

What I want is this: After tableViewCells shown on the screen at given size (say 300 * 50 ), the user can tap some cell to show certain sized image (say 600 * 600 ). 我想要的是:在给定大小(例如300 * 50 )的屏幕上显示tableViewCells之后,用户可以点击一些单元格以显示特定大小的图像(例如600 * 600 )。 To update the cell, I use tableView.beginUpdates() and tableView.endUpdates() . 要更新单元格,我使用tableView.beginUpdates()tableView.endUpdates() Then the tabelViewCell's size should changed to 300 * 300 . 然后,tabelViewCell的大小应更改为300 * 300

After so many tries, I face the problem below: 经过多次尝试,我面临以下问题:

If I set the imageView's contentmode to aspectFit, the tableViewCell's size and imageView's size both became 300 * 600 (the actual image is 300 * 300 ), with blank area at the cell's top and bottom regions, like this: 如果我将imageView的contentmode设置为AspectFit,则tableViewCell的大小和imageView的大小都变为300 * 600 (实际图像为300 * 300 ),在单元格的顶部和底部区域有空白区域,如下所示: contentmode = AspectFit

If I set the imageView's contentmode to aspectFill, the tableViewCell's size and imageView's size both became 300 * 600 (the actual image is 600 * 600 ), with left side and right side is clipped off, like this: 如果将imageView的contentmode设置为AspectFill,则tableViewCell的大小和imageView的大小都变为300 * 600 (实际图像为600 * 600 ),左右两侧都被剪掉,像这样: contentmode = AspectFill

Is there anyway to make the tableViewCell best fit the image while keeping aspect ratio? 无论如何,要使tableViewCell最适合图像,同时保持宽高比? Thanks a lot. 非常感谢。

When you load an image, calculate the images aspect ratio. 加载图像时,请计算图像的纵横比。

Then add a constraint to the UIImageView to fix its aspect ratio. 然后将约束添加到UIImageView以固定其宽高比。 Aspect ratio constraints can not be changed once created, so you have to remove any existing one and add a new one every time you change the image. 宽高比约束一旦创建便无法更改,因此,每次更改图像时,您都必须删除任何现有的约束并添加新的约束。 Something like: 就像是:

// Declare constraint as a property so you can remove it
@property NSLayoutConstraint *constraint;

// Remove previous constraint as multiplier property is read only.
if (self.constraint){
  [self.imageView removeConstraint:self.constraint];
}

// Calculate aspect ration
CGFloat aspectRation = <insert calculation...>

// Add a new constraint for the new image setting the aspect ration
self.constraint = [NSLayoutConstraint constraintWithItem:self.imageView  
                                          attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual 
                                             toItem:self.imageView 
                                          attribute:NSLayoutAttributeWidth 
                                         multiplier:aspectRatio constant:0.0f];

// Add the new constraint.
[self.imageView addConstraint:constraint];

// View needs updated.
[self.contentView setNeedsLayout];

This should force the image to have and aspect ratio calculated. 这将强制图像具有和计算出的宽高比。

So that the image view knows its width or height(but not both as that would clash with aspect ratio), you have to add constraints for the image view to meet your display needs. 为了使图像视图知道其宽度或高度(但不能同时知道两者的宽高比),因此必须为图像视图添加约束以满足显示需求。 eg You might want to make it have equal width with the containing view so it always fills the full width and the height will then change based on aspect ratio etc... This is down to how you want the image to fill the cell. 例如,您可能希望使其与包含的视图具有相等的宽度,因此它始终会填充整个宽度,然后高度将根据长宽比等进行更改。这取决于您希望图像填充单元格的方式。

If using storyboard, you may find it easier to give the UIImageView an aspect ratio so that IB does not complain about missing constraints. 如果使用情节提要,您可能会发现为UIImageView提供宽高比会更容易,这样IB不会抱怨缺少约束。 CTRL-drag that into your cell header so you have an IBOutlet for that constraint. 按住CTRL键并将其拖动到单元头中,以便为该约束设置一个IBOutlet。 First time around, remove the IB version from the UIImageView. 第一次,从UIImageView中删除IB版本。

Hope this code will help you 希望这段代码对您有帮助

cimageView.contentMode = UIViewContentModeCenter;
 if (imageView.bounds.size.width > ((UIImage*)imagesArray[i]).size.width && imageView.bounds.size.height > ((UIImage*)imagesArray[i]).size.height) {
   imageView.contentMode = UIViewContentModeScaleAspectFit;

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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