ios Swift UIImageView 拉伸方面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27961884/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 04:22:25  来源:igfitidea点击:

Swift UIImageView Stretched Aspect

iosobjective-cswiftuiimageview

提问by George Grover

UIImageView renders the size of an image incorrectly. Using Scale Aspect Fit, if the UIImageView is a square the image is the correct aspect ratio with transparency in the areas the image does not fill.

UIImageView 错误地呈现图像的大小。使用 Scale Aspect Fit,如果 UIImageView 是正方形,则图像是正确的纵横比,图像未填充的区域具有透明度。

//Image is Square & Correct Size
var imageView = UIImageView(frame: CGRectMake(0, 50, 320, 320))
imageView.clipsToBounds = true
imageView.contentMode = UIViewContentMode.ScaleAspectFit

//Image is Rectangle & Incorrect Size
var imageView = UIImageView(frame: CGRectMake(0, 50, 320, 450))
imageView.clipsToBounds = true
imageView.contentMode = UIViewContentMode.ScaleAspectFit

The UIImageView needs to touch the edges and have transparent space at the top and bottom of the screen and the image inside needs to keep its original ratio rather than stretching taller. I have attached two images of how the image inside the UIImageView is rendering.

UIImageView 需要触摸边缘并在屏幕的顶部和底部有透明空间,并且里面的图像需要保持其原始比例而不是拉伸得更高。我附上了两张关于 UIImageView 内部图像渲染方式的图像。

ImageView with correct AspectImageView with wrong Aspect

具有正确 Aspect 的 ImageViewImageView 有错误的方面

回答by George Grover

I added an autoresizing mask to the UIImageView and it now displays the correct ratios.

我向 UIImageView 添加了一个自动调整大小的掩码,它现在显示正确的比例。

imageView.autoresizingMask = UIViewAutoresizing.FlexibleBottomMargin | UIViewAutoresizing.FlexibleHeight | UIViewAutoresizing.FlexibleRightMargin | UIViewAutoresizing.FlexibleLeftMargin | UIViewAutoresizing.FlexibleTopMargin | UIViewAutoresizing.FlexibleWidth
imageView.contentMode = UIViewContentMode.ScaleAspectFit

This answer helped me: Captured photo is stretched with AVCaptureSession sessionPreset = AVCaptureSessionPresetPhotoas I was using an image that was taken through the phones camera.

这个答案对我有帮助:捕获的照片使用 AVCaptureSession sessionPreset = AVCaptureSessionPresetPhoto 拉伸,因为我使用的是通过手机相机拍摄的图像。

回答by Paul G

I was having the same issue, and what fixed it for me was making sure to set the imageView image after the content mode was set. ie:

我遇到了同样的问题,对我来说修复它的是确保在设置内容模式后设置 imageView 图像。IE:

    self.imageView.contentMode = UIViewContentMode.ScaleAspectFit
    self.imageView.image = imageChosen

Cheers

干杯

回答by Frank Dev

Swift 3version of your code:

Swift 3版本的代码:

imageView.autoresizingMask = [.flexibleWidth, .flexibleHeight, .flexibleBottomMargin, .flexibleRightMargin, .flexibleLeftMargin, .flexibleTopMargin]
imageView.contentMode = .scaleAspectFit // OR .scaleAspectFill
imageView.clipsToBounds = true

回答by Cezar

That's the intended behaviour for UIViewContentMode.ScaleAspectFit. From the docs:

这是 的预期行为UIViewContentMode.ScaleAspectFit。从文档:

UIViewContentModeScaleAspectFit

The option to scale the content to fit the size of the view by maintaining the aspect ratio. Any remaining area of the view's bounds is transparent.

UIViewContentModeScaleAspectFit

通过保持纵横比来缩放内容以适应视图大小的选项。视图边界的任何剩余区域都是透明的。

It seems from what you describe that you need UIViewContentMode.ScaleAspectFill

从你描述的情况看来,你需要 UIViewContentMode.ScaleAspectFill

UIViewContentModeScaleAspectFill

The option to scale the content to fill the size of the view. Some portion of the content may be clipped to fill the view's bounds.

UIViewContentModeScaleAspectFill

缩放内容以填充视图大小的选项。内容的某些部分可能会被裁剪以填充视图的边界。

回答by Hamid Hoseini

There is a great solution here:by olearyj234 .
and it helped me a lot. I suggest taking a look. Also, here is his code in swift 4 and Xcode 9.2:

这里有一个很好的解决方案通过olearyj234
它对我帮助很大。我建议看看。此外,这是他在 swift 4 和 Xcode 9.2 中的代码:

    class ScaledHeightImageView: UIImageView {

    override var intrinsicContentSize: CGSize {

        if let myImage = self.image {
            let myImageWidth = myImage.size.width
            let myImageHeight = myImage.size.height
            let myViewWidth = self.frame.size.width

            let ratio = myViewWidth/myImageWidth
            let scaledHeight = myImageHeight * ratio

            return CGSize(width: myViewWidth, height: scaledHeight)
        }

        return CGSize(width: -1.0, height: -1.0)
    }

}

回答by álvaro Agüero

Swift 5

斯威夫特 5

You can apply UIView.ContentMode.scaleAspectFitonly for UIImageViewlike this:

您可以 像这样为UIImageView应用UIView.ContentMode.scaleAspectFit

        let logoImage:UIImage = UIImage(named: "my_logo")!

        let logoImageView = UIImageView(image: logoImage)

        logoImageView.contentMode = UIView.ContentMode.scaleAspectFit

        return logoImageView