[英]UIImage in a Rounded UIView swift
我創建了一個UIView,並在UIView中添加了一個Imageview。 我給了UIView角半徑,它可以工作,現在的問題是放置在其中的圖像不考慮視圖的角半徑,因此,它將整個圖像顯示為矩形,而不是具有左上角和topRight角彎曲到與UIView相同的半徑。 我的代碼如下所示。
let bgView: UIView = {
let bgView = UIView()
bgView.backgroundColor = .white
return bgView
}()
let propertyImage: DefaultImageView = {
let img = DefaultImageView(frame: .zero)
img.clipsToBounds = true
img.layer.masksToBounds = true
img.image = #imageLiteral(resourceName: "icons8-name-filled-30")
img.contentMode = .scaleToFill
return img
}()
這給出了四舍五入的卡片視圖
extension UIView {
func addShadowAndRoundCorners() {
layer.shadowOpacity = 1
layer.shadowOffset = CGSize.zero
layer.shadowColor = UIColor.darkGray.cgColor
layer.cornerRadius = 12
}
}
所以我添加了bgView.addShadowAndRoundCorners
類的圓形樣式
如何使imageview與UIView的圓角保持一致
將clipsToBounds設置為yes時,也需要將其添加到容器視圖中,以強制子視圖不呈現在外部:
bgView.clipsToBounds = true
或者,您可以只對imageView本身進行四舍五入。
如果只希望對UIImageView的topLeft和topRight角進行四舍五入,則可能必須在顯示圖像之前使用遮罩。
創建一個與圖像大小相同的CGContext,在CGContext中創建遮罩,然后將圖像繪制到上下文中,將應用遮罩,然后從上下文中獲取新圖像並將其放入UIImageView的.image中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.