【问题标题】:iOS Clean corners for rounded profile imageiOS 为圆形轮廓图像清洁角落
【发布时间】:2015-03-03 02:12:16
【问题描述】:

所以我按照AppCoda 教程对个人资料图像的角进行了圆角处理,它工作得很好,除了一件事。无论图像在哪里进行了圆角处理,图像都会有一点渗出(尤其是在使用白色边框的情况下)。

self.imageview.image = image
self.imageview.layer.cornerRadius = 10.0
self.imageview.layer.borderWidth = 3.0
self.imageview.layer.borderColor = UIColor.whiteColor().CGColor
self.imageview.clipsToBounds = true

【问题讨论】:

  • 只是一个建议:您是否尝试将视图的背景颜色设置为清晰颜色?
  • 刚刚试过,没有效果
  • 试试 imageview.layer.masksToBounds = YES;
  • 我希望这有效....

标签: ios uiview


【解决方案1】:

如果需要,您还可以添加一个稍微插入的遮罩:

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

【讨论】:

    【解决方案2】:

    您可以在矩形上创建一个蒙版。这似乎给出了清晰的边缘,至少在 Playground 中是这样。这是代码,但您需要对其进行一些修改以获得四舍五入的内部矩形。

    // 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
    

    【讨论】:

      【解决方案3】:

      一个简单的解决方案是您可以稍微扩大图层的边界以覆盖视图图像的边缘:

      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)];
      

      【讨论】:

        猜你喜欢
        • 2012-07-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-23
        • 2020-05-23
        • 1970-01-01
        • 2015-04-26
        • 1970-01-01
        相关资源
        最近更新 更多