iOS Clean corners for rounded profile image

前端 未结 3 589
野趣味
野趣味 2020-12-20 02:28

So I followed an AppCoda tutorial on rounding the corners of a profile image, and it worked fine, except for one thing. Wherever the image was rounded, there is a bit of bl

相关标签:
3条回答
  • 2020-12-20 03:14

    You can also add a mask which is inset a little, if you want:

    let path = UIBezierPath(roundedRect: CGRectInset(imageView.bounds, 0.5, 0.5), cornerRadius: 10.0)
    let mask = CAShapeLayer()
    mask.path = path.CGPath
    imageview.layer.mask = mask
    
    0 讨论(0)
  • 2020-12-20 03:15

    A simple solution is that you can enlarge layer's bounds a little bit to cover the edge of view's image:

    CGFloat offset = 1.f; // .5f is also good enough
    self.imageview.image = image;
    self.imageview.layer.cornerRadius = 10.0;
    self.imageview.layer.borderWidth = 3.0 + offset;
    self.imageview.layer.borderColor = UIColor.whiteColor().CGColor;
    self.imageview.layer.masksToBounds = YES;
    
    [self.imageview.layer setBounds:CGRectMake(-offset,
                                               -offset,
                                               CGRectGetWidth(self.imageview.frame)  + offset * 2.f,
                                               CGRectGetHeight(self.imageview.frame) + offset * 2.f)];
    
    0 讨论(0)
  • 2020-12-20 03:23

    You could create a mask over the rectangle. This seems to give clean edges, at least in Playground. Here is the code, but you will need to modify it a bit to get rounded inner rect.

    // simple red rect
    var view = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
    view.backgroundColor = UIColor.redColor()
    view.layer.borderColor = UIColor.whiteColor().CGColor
    view.layer.borderWidth = 6.0
    
    // path for the mask
    let rectanglePath = UIBezierPath(roundedRect:view.bounds, cornerRadius: 20)
    // applying the mask over the view
    let maskLayer = CAShapeLayer()
    maskLayer.frame = view.bounds
    maskLayer.path = rectanglePath.CGPath
    view.layer.mask = maskLayer
    
    0 讨论(0)
提交回复
热议问题