UIImageView无法在自定义单元格iOS 8中正确调整大小

时间:2015-03-02 02:31:35

标签: ios uitableview ios8

enter image description here

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

我有一个标签,它包含上面的标题以及UIImageView,在图像下面我有共享图标等。我已经将图像的约束设置为UIImageView以及较低的共享图标关于前导和尾随空间。看起来我已经尝试了切换视图的“模式”(即方面填充,宽高比适合)的每个组合,当我做方面适合它正确地调整图像大小但是在下部和上部添加了一堆填充。图片(下面的截图)enter image description here

标签和图标都有适当的约束集(据我所知)

我已经搜索了几个小时试图找到有类似问题的人,但无济于事我似乎无法找到任何关于使用可能大小不同的图像的自我调整大小的内容。

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

3 个答案:

答案 0 :(得分: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的应用程序动态调整图像大小的方法。希望这会有所帮助。

答案 1 :(得分:1)

这是由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

答案 2 :(得分:0)

我有同样的问题。

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

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

我现在的解决方法是手动调整图像大小,但这非常昂贵,而且还会在我的UI中产生一些其他问题。

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