UIImageView:结合UIViewContentModeScaleAspectFit和UIViewContentModeBottom

时间:2011-02-16 10:41:59

标签: iphone uiimageview

有没有办法避免UIImageView中的图像拉伸并同时将其对齐到底部?基本上我想知道如果可以将UIViewContentModeScaleAspectFitUIViewContentModeBottom contentMode组合起来会发生什么。

5 个答案:

答案 0 :(得分:1)

自己找到解决方案。此代码会将UIImageView调整为image的大小,并将其移至超级视图的底部。

CGSize initialImageSize = imageView.size;

CGSize imageSize = imageView.image.size;
CGFloat aspectRatio = imageSize.width / imageSize.height;

CGRect imageFrame = imageView.frame;

if (initialImageSize.width / aspectRatio <= initialImageSize.height) {
    imageFrame.size.width = initialImageSize.width;
    imageFrame.size.height = imageFrame.size.width / aspectRatio;
} else {
    imageFrame.size.height = initialImageSize.height;
    imageFrame.size.width = imageFrame.size.height * aspectRatio;
}

CGRect parentFrame = imageView.superview.frame;
imageFrame.origin.y = (parentFrame.origin.y + parentFrame.size.height) - imageFrame.size.height;

imageView.frame = imageFrame;

答案 1 :(得分:1)

基于约束的方法

您希望约束left的{​​{1}},rightbottom边,同时确保尺寸的比例与它显示的UIImageView

UIImage

这将模拟UIImage *image; // allocate image object here UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; [parent addSubview:imageView]; // add imageView to proper view with addSubview: here UIView *superview = imageView.superview; // match width and bottom of self.superview CGFloat ratio = image.size.width / image.size.height; NSLayoutConstraint *leftConstraint = [NSLayoutConstraint constraintWithItem:imageView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:superview attribute:NSLayoutAttributeLeft multiplier:1.f constant:0.f]; NSLayoutConstraint *bottomConstraint = [NSLayoutConstraint constraintWithItem:imageView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:superview attribute:NSLayoutAttributeBottom multiplier:1.f constant:0.f]; NSLayoutConstraint *rightConstraint = [NSLayoutConstraint constraintWithItem:imageView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:superview attribute:NSLayoutAttributeRight multiplier:1.f constant:0.f]; NSLayoutConstraint *ratioConstraint = [NSLayoutConstraint constraintWithItem:imageView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:imageView attribute:NSLayoutAttributeHeight multiplier:ratio constant:1.f]; [imageView addConstraint:ratioConstraint]; [superview addConstraints:@[leftConstraint, bottomConstraint, rightConstraint]]; UIViewContentModeScaleAspectFit的组合,但也会调整UIViewContentModeBottom的高度。

答案 2 :(得分:0)

我的解决方案:

    float newwidth;
    float newheight;

    if (image.size.height>=image.size.width){
        newheight=imageview.frame.size.height;
        newwidth=(image.size.width/image.size.height)*newheight;

        if(newwidth>imageview.frame.size.width){
            float diff=imageview.frame.size.width-newwidth;
            newheight=newheight+diff/newheight*newheight;
            newwidth=imageview.frame.size.width;
        }

    }
    else{
        newwidth=imageview.frame.size.width;
        newheight=(image.size.height/image.size.width)*newwidth;

        if(newheight>imageview.frame.size.height){
            float diff=imageview.frame.size.height-newheight;
            newwidth=newwidth+diff/newwidth*newwidth;
            newheight=imageview.frame.size.height;
        }
    }

答案 3 :(得分:0)

我找到了一种更简单的方法。

imageView.contentMode = .Bottom
imageView.clipToBounds = true

这将阻止图像调整大小,无论它受限于什么帧。而clipToBounds部分很重要,因为它可以防止非调整大小的图像超出imageView边界。

答案 4 :(得分:0)

晚了聚会,但是很简单的解决方案。在UIImageView上:

  • 使用contentMode属性定位图像。 (在当前情况下为UIView.ContentMode.bottom)
  • 使用UIImageView的contentScaleFactor属性缩放内容视图中的图像。

这将保持纵横比,只需计算所需的比例即可。对于scaleAspectFit:

yourImageView.contentMode = UIView.ContentMode.bottom let relWidthScale = yourImage.size.width/yourImageView.frame.width let relHeightScale = yourImage.size.height/yourImageView.frame.height yourImageView.contentScaleFactor = min(relWidthScale,relHeightScale)

如果UIImageView更改了大小,则可以通过向UIImageView或其容器的bounds属性添加观察者来更新比例。