[英]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:这是在不同模拟器设备中的外观示例:
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.