Iphone UIImageView:组合UIViewContentModeScaleAspectFit和UIViewContentModeBottom

Iphone UIImageView:组合UIViewContentModeScaleAspectFit和UIViewContentModeBottom,iphone,uiimageview,Iphone,Uiimageview,是否有办法避免图像在UIImageView中拉伸,同时将其与底部对齐?基本上,我想知道如果可以将UIViewContentModeScaleAspectFit和UIViewContentModeBottomcontentModes组合起来,会发生什么情况。我自己找到了解决方案。此代码将UIImageView的大小调整为其图像的大小,并将其移动到superview的底部 CGSize initialImageSize = imageView.size; CGSize imageSize = im

是否有办法避免图像在
UIImageView
中拉伸,同时将其与底部对齐?基本上,我想知道如果可以将
UIViewContentModeScaleAspectFit
UIViewContentModeBottom
contentMode
s组合起来,会发生什么情况。

我自己找到了解决方案。此代码将
UIImageView
的大小调整为其
图像的大小,并将其移动到superview的底部

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;
CGSize initialImageSize=imageView.size;
CGSize imageSize=imageView.image.size;
CGFloat aspectRatio=imageSize.width/imageSize.height;
CGRect imageFrame=imageView.frame;
如果(initialImageSize.width/aspectRatioMy solution:

    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;
        }
    }

我找到了一个更简单的方法

imageView.contentMode = .Bottom
imageView.clipToBounds = true
这将阻止图像调整大小,无论它在哪一帧内。clipToBounds部分很重要,因为它防止未调整大小的图像超出imageView边界。

基于约束的方法 您需要约束
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
的组合,但也将调整
UIImageView
的高度。

晚到派对,但这是一个简单的解决方案。在UIImageView上:

  • 使用contentMode属性定位图像。(在当前情况下为UIView.contentMode.bottom)
  • 使用UIImageView的contentScaleFactor属性在内容视图中缩放图像
这将保持纵横比,只需计算所需的比例。对于ScaleSpectFit:

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


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

任何不让图像视图移动的方法,只需将图像推到UIImageView的底部即可。发布代码时,最好添加一个简短的说明,说明它的作用以及它如何区分从其他解决方案中获取更多信息。