【发布时间】:2017-01-09 10:44:55
【问题描述】:
在自定义UIView 中,我已经覆盖了draw() 函数。我需要以圆形布局绘制图像。另外,如果可能的话,我需要调整图像的大小以保持 aspect fill/fit 属性。该怎么做?
代码:
@IBDesignable class ScoreGraphView: UIView {
override func draw(_ rect: CGRect) {
let iv = UIImageView(image: UIImage(named: "women"))
iv.frame = CGRect(x: 0, y: 0, width: rect.width, height: rect.height)
iv.layer.cornerRadius = 20
iv.clipsToBounds = true
iv.layer.masksToBounds = true
iv.backgroundColor = UIColor.clear
iv.draw(CGRect(x: 0, y: 0, width: rect.width, height: rect.height))
}
}
以上这些线可用于绘制图像。 cornerRadius/masksToBounds 不工作。
提前致谢!
【问题讨论】:
-
cornerRadius仅在您还设置了masksToBounds = true时才有效 -
@Tj3n 你的意思是 clipToBounds = true 吗?我也试过了,但没有运气。
-
iv.layer.masksToBounds = true不起作用?它对我来说很好,也许在你的draw函数中你覆盖了一些东西? -
@Tj3n 我已经发布了自定义 uiview 的完整源代码,它仍在绘制完整的矩形图像。你有什么线索吗?
-
尝试删除
iv.draw(CGRect(x: 0, y: 0, width: rect.width, height: rect.height))
标签: ios swift uiimage core-graphics uibezierpath