私はSDWebImage
でダウンロードした背景のボタンを持っています。私は高さを一定に保ち、アスペクト比を維持し、画像をカットしないで幅を変えたい。一定の高さと動的な幅。ここで何が間違っていますか?SDWebImageボタン背景イメージサイズ変更
//CODE FROM FUNCTION ---
button.sd_setBackgroundImage(with: url, for: .normal, placeholderImage: UIImage(named: "img"),options: SDWebImageOptions(rawValue: 0), completed: { (image, error, cacheType, imageURL) in
let view = UIView(frame: CGRect(x: 0, y: 0, width: self.resizeImage(image: image!, newHeight: 100), height: 100))
button.frame = view.frame
})
view.addSubview(button)
return view
これは、あなたがボタンの幅についてIBoutlet NSLayoutConstraintを作成し、(イメージをダウンロードした後)画像の幅ごとに実行時にボタンの幅に対して一定の値を与えることができ、私のヘルパー関数
func resizeImage(image: UIImage, newHeight: CGFloat) -> CGFloat {
let scale = newHeight/image.size.height
let newWidth = image.size.width * scale
return newWidth
}
あなたが見ている出力は何ですか、スクリーンショットを追加しますか? –
これは私に今間違いを与えているので、間違った経路に完全に入っています '理由: 'layerはレイヤーツリーのサイクルの一部です' –
Kenny