【问题标题】:UIImageView not correctly resizing in self sizing cells iOS 8UIImageView 在 iOS 8 的自定尺寸单元格中未正确调整大小
【发布时间】:2015-07-23 10:16:55
【问题描述】:

我正在尝试实现类似于 9gag 应用程序的功能,其中单元格会根据图像的大小自动调整大小。宽度紧贴两侧,但高度根据需要调整大小。

我有一个标签,它由其上方的标题以及 UIImageView 和图像下方我有共享图标等组成。我已将图像的约束设置为 UIImageView 和下部共享图标至于前导和尾随空格。似乎我已经尝试了切换视图“模式”的所有组合(即方面填充,方面适合),当我进行方面适合时,它会正确调整图像的大小,但会在下部和上部添加一堆填充图片(截图如下)

标签和图标都设置了适当的约束(据我所知)

我已经搜索了几个小时试图找到有类似问题的人,但无济于事

谁能指出我正确的方向或知道我的问题的解决方案?一如既往地非常感谢所有帮助!

【问题讨论】:

    标签: ios uitableview ios8


    【解决方案1】:

    据我所知,您不能真正从 Interface Builder 调整图像大小。您必须在 ViewController.m 文件中手动编写代码。有这样的东西:

    - (void) adjustImageSize{
        CGSize imageSize = self.imageView.image.size;
        CGSize imageViewSize = self.imageView.frame.size;
        float imageAspectRatio = imageSize.width/imageSize.height;
        CGRect frame = self.statusImage.frame;
        frame.size.height =imageViewSize.width/imageAspectRatio;
        self.imageView.frame = frame;
        // set your Cell's frame here, based on your other constraints (labels/buttons etc), after you have resized your image
    }
    

    您可以从 tableView 数据源函数中调用它。然后添加这个委托函数来告诉tableView每个单元格的动态高度。

    - (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
        UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
        return cell.frame.size.height;
    }
    

    这就是我为基于 NewsFeed 的应用程序动态调整图像大小的方法。希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      这是由于UIImageView 固有内容大小(图像大小)造成的。 虽然您的图像视图的宽度通过前导和尾随空间限制调整大小,但没有任何限制它的高度,因此它保持等于原始图像高度。

      解决方案是计算图像的纵横比并将其设置为UIImageView宽度的比率约束

      @interface PostImageViewCell()
      
      @property (nonatomic, strong) NSLayoutConstraint *aspectConstraint;
      
      @end
      
      @implementation PostImageViewCell
      
      - (void) setAspectConstraint:(NSLayoutConstraint *)aspectConstraint {
      
          if (_aspectConstraint != nil) {
              [self.postImage removeConstraint:_aspectConstraint];
              _aspectConstraint = nil;
          }
          if (aspectContraint != nil) {
              [self.postImage addConstraint:aspectConstraint];
              _aspectConstraint = aspectConstraint
          }
      }
      
      - (void)setPicture:(UIImage *)image {
      
          CGFloat aspect = image.size.width / image.size.height;
          self.aspectConstraint = [NSLayoutConstraint constraintWithItem:self.postImage
                                                               attribute:NSLayoutAttributeWidth
                                                               relatedBy:NSLayoutRelationEqual
                                                                  toItem:self.postImage
                                                               attribute:NSLayoutAttributeHeight
                                                              multiplier:aspect
                                                                constant:0.0];
          [self.postImage setImage:image];
      }
      
      @end
      

      【讨论】:

        【解决方案3】:

        我也有同样的问题。

        我注意到单元格将根据您的 imageView 中的图像计算其大小,并且它忽略了 UIImageView 将更改图像的大小以使其适合其边界。

        示例:您的图片大小为 1000x1000。如果您将内容模式设置为“Aspect Fit”,则 UIImageView 将显示具有自身宽度的图像。因此显示的图像可能具有您的 iPhone 的宽度 - 例如750px 和高度 750px(它是一个正方形)。 现在单元格将自行调整大小,就好像 imageView 将以原始格式显示图像一样; 1000x1000。这会导致不需要的空白。

        我现在的解决方法是手动缩小图像的大小,但这非常昂贵,而且还会给我的 UI 带来一些其他问题。

        你找到更好的解决方案了吗?

        【讨论】:

        • 不幸的是我没有找到更好的解决方案。我实际上意识到你在调整图像大小时所做的事情是一样的。我正在努力在图像进入查看之前对其进行预缓存,因为图像在进入查看后会按比例缩小。您是否实施了任何预缓存?
        • 所以现在我正在缓存图像的纵向和横向版本,效果很好。这可能会占用大量内存,但动态计算图像会影响 UI 性能。
        猜你喜欢
        • 2014-11-12
        • 2015-02-26
        • 1970-01-01
        • 2013-02-03
        • 2020-12-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多