簡體   English   中英

如何調整所有 iOS 設備的圖像大小?

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

我有一個 UITableViewCell,里面有一個 UIImageView。 在這個 class 中,我從服務器加載了一個圖像,但問題是該圖像在某些設備中看起來非常拉長。 如何在所有設備中實現圖像尺寸看起來相等?

這是我的 UITableViewCell 和 UIImageView 配置。 表視圖

圖像視圖

這是我用來將圖像從 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))
    }
}

這是在不同模擬器設備中的外觀示例:

iPhone 13 迷你

iPhone 13 專業版

iPod 觸摸 7

iPhone 8

解決此問題的任何建議(?)

謝謝你。

自從我在 IB 中做約束以來已經有很長時間了,但是您需要將寬度約束設置為高度約束的倍數。 這將使圖像比例保持恆定。

如果你是 6:9 的圖像,你會設置為 0.66。 在代碼中它看起來像這樣

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

你可以在 IB 中做同樣的事情。

您甚至可以通過 IBOutlets 將寬度和高度約束暴露到您的 viewController 中並使用上述方法。 但是混合 IB 和代碼約束通常不是一個好主意,因為當您將來查看它時,很難弄清楚正在發生的事情。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM