在图像的边界周围切碎边缘

时间:2016-12-16 06:25:37

标签: ios

我有一个带边框的图片

let smallicon: UIImageView = {
    let smallicon = UIImageView()
    smallicon.layer.borderWidth = 2
    smallicon.layer.borderColor = UIColor.whiteColor().CGColor
    smallicon.hidden = true
    return smallicon
}()

问题是边界周围有微小的切边(黄色和黑色的小图像)

enter image description here

如何摆脱它?

1 个答案:

答案 0 :(得分:1)

解决方案

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    // Create a view with red background for demonstration
    let v = UIView(frame: CGRectMake(0, 0, 100, 100))
    v.center = view.center
    v.backgroundColor = UIColor.redColor()
    view.addSubview(v)

    // Add rounded corners
    let maskLayer = CAShapeLayer()
    maskLayer.frame = v.bounds
    maskLayer.path = UIBezierPath(roundedRect: v.bounds, byRoundingCorners: .TopRight | .TopLeft, cornerRadii: CGSize(width: 25, height: 25)).CGPath
    v.layer.mask = maskLayer

    // Add border
    let borderLayer = CAShapeLayer()
    borderLayer.path = maskLayer.path // Reuse the Bezier path
    borderLayer.fillColor = UIColor.clearColor().CGColor
    borderLayer.strokeColor = UIColor.greenColor().CGColor
    borderLayer.lineWidth = 5
    borderLayer.frame = v.bounds
    v.layer.addSublayer(borderLayer)   
}

}

请注意!为了使这个工作目标视图必须具有frame属性设置大小。如果没有大小,您的视图根本不会被看到