简体   繁体   English

如何调整所有 iOS 设备的图像大小?

[英]How can I resize the image for all iOS devices?

I have an UITableViewCell and inside that I have a UIImageView.我有一个 UITableViewCell,里面有一个 UIImageView。 In this class I load a image from the server but the problem is that that image looks very elongated in some devices.在这个 class 中,我从服务器加载了一个图像,但问题是该图像在某些设备中看起来非常拉长。 How can I achieve to the image size looks equals in all devices?如何在所有设备中实现图像尺寸看起来相等?

This is my UITableViewCell and my UIImageView configuration.这是我的 UITableViewCell 和 UIImageView 配置。 表视图

图像视图

And this is the code that I'm using to set the image from model to the ImageView:这是我用来将图像从 model 设置为 ImageView 的代码:

func setElementsWithModel(model : XSModelMarca?, position: Int) -> Void {
    self.index = position
    var frameworkBundle:Bundle? {
        let bundleId = "com.exagono.dcnetfw.DCNetFW"
        return Bundle(identifier: bundleId)
    }
    if model != nil {
        if #available(iOS 13.0, *) {
            self.mImgBackground.image = UIImage(named: XSUtils.getBackgroudWithClave(marca: model!.mMarca), in: frameworkBundle, with: .none)
        } else {
            self.mImgBackground.image = UIImage(named: XSUtils.getBackgroudWithClave(marca: model!.mMarca), in: frameworkBundle, compatibleWith: nil)
        }
        self.priceButton.backgroundColor = UIColor(hexString: XSUtils.getColorWithMarca(marca: model!.mMarca))
        self.modelButton.backgroundColor = UIColor(hexString: XSUtils.getColorWithMarca(marca: model!.mMarca))
    }
}

And this is the example how looks in different Simulator Devices:这是在不同模拟器设备中的外观示例:

iPhone 13 迷你

iPhone 13 专业版

iPod 触摸 7

iPhone 8

Any suggestions for resolve this problem (?)解决此问题的任何建议(?)

Thank you.谢谢你。

It's been a long time since I did constraints in IB, but you need to set the width constraint to a multiple of the height constraint.自从我在 IB 中做约束以来已经有很长时间了,但是您需要将宽度约束设置为高度约束的倍数。 This will keep the image ratio at a constant ratio.这将使图像比例保持恒定。

If you a 6:9 image you'd set it 0.66.如果你是 6:9 的图像,你会设置为 0.66。 In code it would look something like this在代码中它看起来像这样

NSLayoutConstraint.activate([
         myview.heightAnchor.constraint(equalTo: myview.widthAnchor, multiplier: 0.66)
])

You can do the same in IB.你可以在 IB 中做同样的事情。

You could even expose the width and height constraints into your viewController through IBOutlets and use the above.您甚至可以通过 IBOutlets 将宽度和高度约束暴露到您的 viewController 中并使用上述方法。 BUT mixing IB and code constraints is not a good idea generally as it makes it hard to work out what's happening when you look at it in the future.但是混合 IB 和代码约束通常不是一个好主意,因为当您将来查看它时,很难弄清楚正在发生的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 iOS:所有设备的图片自动调整大小 - iOS: Image auto resize for all devices 如何使我的iOS应用程序正确调整为所有iphone设备的大小? - How do I make my iOS application resize correctly to all iphone devices? 上传所有移动设备时如何调整图像大小? - How to resize image when upload for all mobile devices? 如何为所有Apple设备调整图像资产的大小? (不启动图像) - How to resize image assets for all Apple devices? (not launch images) 调整所有设备的iOS App Preview的大小 - Resize iOS App Preview for all devices iOS 背景图片分辨率:如何支持所有 iOS 设备 - iOS background image resolution: How to support all iOS devices 如何解决 iOS 设备的错误调整大小行为 - How to solve erroneous resize behavior of iOS devices 如何调整iOS中完整视图的大小-从iPad到iPhone,并按比例缩放所有内容? - How can I resize a complete view in iOS - from iPad to iPhone with all content proportionally? 如何在表格视图中调整图像大小? - How can i resize image in table view? 如何为所有不同尺寸的 ios 设备制作相同尺寸的画布,画布具有文本和贴纸等元素 - swift ios - How can I make same size canvas for all different size of ios devices , canvas has elements like text and stickers - swift ios
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM