简体   繁体   English

如何在自定义tableview单元格下调整UIImageview

[英]How to adjust the UIImageview under custom tableview cell

I have a custom tableview and under each cell i have image-view. 我有一个自定义的tableview,在每个单元格下都有image-view。 I am receiving images of different sizes from the service and i want to adjust my frame according to that. 我正在从服务中接收不同尺寸的图像,并且我想根据该图像调整帧。

Whenever there is a picture smaller than the frame size i want to adjust the height according to its height. 每当有小于边框尺寸的图片时,我都想根据其高度调整高度。

在此处输入图片说明

So i am comparing the current frame size and image size and trying to update the imageview frame,but its not working. 所以我正在比较当前的帧大小和图像大小,并尝试更新imageview帧,但它不起作用。

cell.image.contentMode = UIViewContentModeScaleAspectFit;
        if(cell.image.image.size.height<cell.image.frame.size.height)
        {
        cell.image.frame = CGRectMake(cell.image.frame.origin.x, cell.image.frame.origin.y,cell.image.frame.size.width, cell.image.image.size.height);
        }

what needs to be done to overcome the problem?? 需要做什么来克服这个问题?

If the code above is in cellForRowAtIndexPath and the cell that you have initialized inside cellForRowAtIndexPath is an instance of ItemCell (for instance), then in your ItemCell class, you will have to override layoutSubviews and check the size of the image subview inside layoutSubviews and adjust the height of the frame of the cell accordingly inside layoutSubviews. 如果上面的代码在cellForRowAtIndexPath中,并且您在cellForRowAtIndexPath中初始化的单元格是ItemCell的实例(例如),那么在ItemCell类中,您将必须覆盖layoutSubviews并检查layoutSubviews内图像子视图的大小并进行调整相应的在layoutSubviews内部的单元格框架的高度。 This ensures that every time for cellForRowAtIndexPath is called, the layout of the UITableViewCell subclass (ItemCell) happens before the cell gets rendered by cellForRowAtIndexPath. 这样可以确保每次为cellForRowAtIndexPath调用时,UITableViewCell子类(ItemCell)的布局都会在cellForRowAtIndexPath呈现单元之前发生。

Btw, the fact that you have set the contentMode of the cell to ScaleAspectFit means that regardless of what you do to the frame of the imageView inside the cell, the image will always be scaled to fit the entire view available to it, ie in this case, the dimensions of the imageView inside the cell. 顺便说一句,您已将单元格的contentMode设置为ScaleAspectFit,这意味着无论您对单元格内imageView的框架进行什么操作,图像始终会缩放以适合其可用的整个视图。情况下,单元格内imageView的尺寸。 Maybe you should try moving the contentMode line 也许您应该尝试移动contentMode行

 cell.image.contentMode = UIViewContentModeScaleAspectFit;

after the conditional where you set the frame size of the imageView : 在您设置imageView帧大小的条件之后:

    if(cell.image.image.size.height<cell.image.frame.size.height)
    {
    cell.image.frame = CGRectMake(cell.image.frame.origin.x, cell.image.frame.origin.y,cell.image.frame.size.width, cell.image.image.size.height);
    }
cell.image.contentMode = UIViewContentModeScaleAspectFit;

Ofcourse, this would probably not solve the problem if you aren't overriding layoutSubview to adjust the dimensions of the imageView frame there. 当然,如果您不重写layoutSubview来调整imageView框架的尺寸,则可能无法解决问题。

You would require to change cell height according to image in UITableView datasource like this 您需要像这样在UITableView数据源中根据图像更改单元格高度

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
  if (image.size.width > CGRectGetWidth(self.view.bounds)) {
    CGFloat ratio = image.size.height / image.size.width;
    return CGRectGetWidth(self.view.bounds) * ratio;
  } else {
    return image.size.height;
  }
}

EDIT Incase you are looking out for resizing image and not tableview cell then these are Resize UIImage with aspect ratio? 编辑如果您正在寻找调整图像大小而不是表格视图单元格,那么这些是使用宽高比调整UIImage大小吗? and Resize UIImage with aspect ratio? 使用宽高比调整UIImage的大小? very good references. 很好的参考。 Also you should understand UIImageView Scaling Explained Visually 您还应该了解可视化解释的UIImageView缩放比例

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

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