【问题标题】:SDWebImage button background image resizeSDWebImage 按钮背景图像调整大小
【发布时间】:2017-07-16 17:27:11
【问题描述】:

我有一个带有背景图片的按钮,我使用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

这是我的辅助函数

func resizeImage(image: UIImage, newHeight: CGFloat) -> CGFloat {

    let scale = newHeight / image.size.height
    let newWidth = image.size.width * scale
    return newWidth
}

【问题讨论】:

  • 你看到的输出是什么,添加截图?
  • 它现在给了我一个错误,所以我完全走错了路reason: 'layer <CALayer: 0x60800023cfe0> is a part of cycle in its layer tree

标签: ios swift sdwebimage


【解决方案1】:

您可以为按钮宽度创建IBoutlet NSLayoutConstraint,并在运行时根据图像宽度(下载图像后)为按钮宽度提供恒定值。

【讨论】:

  • 我按照你说的试过了。现在我想我有 2 个相同类型的约束,它会删除新的约束。如何用新的宽度约束替换我的宽度约束?
猜你喜欢
  • 1970-01-01
  • 2017-02-15
  • 1970-01-01
  • 2021-04-04
  • 1970-01-01
  • 2019-10-03
  • 2012-11-04
  • 1970-01-01
  • 2018-01-01
相关资源
最近更新 更多