How to add a border to a circular image with mask

前端 未结 2 932
别跟我提以往
别跟我提以往 2021-01-14 06:54

This is my attempt:

func round() {
    let width = bounds.width < bounds.height ? bounds.width : bounds.height
    let mask = CAShapeLayer()
    mask.path         


        
2条回答
  •  情话喂你
    2021-01-14 07:16

    The easiest way is to manipulate the layer of the image view itself.

    imageView.layer.cornerRadius = imageView.bounds.size.width / 2.0
    imageView.layer.borderWidth = 2.0
    imageView.layer.borderColor = UIColor.whiteColor.CGColor
    imageView.layer.masksToBounds = true
    

    You can include this in viewDidLayoutSubviews or layoutSubviews to make sure the size is always correct.

    NB: Maybe this technique makes your circle mask obsolete ;-). As a rule of thumb, always choose the simplest solution.

提交回复
热议问题