Search code examples
swiftuiimageviewuiimagerounded-cornerscornerradius

How to give corner radius in one side to Imageview in swift?


How can I give corner radius in one side to Imageview like zig zag? like in image Image

In this image I want like a large image witch is highlighted in Blue line.

If anyone know about this Please help me. And I apologize if I could not explane my problem properly.


Solution

  • Solve the problem using bezier path

    Study the bezier path principle and find the appropriate control point

    Here is the sample code

    let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height/3))
    
    let path = UIBezierPath()
           
    path.move(to: CGPoint(x: 0.0, y: 0.0))
    path.addLine(to: CGPoint(x: self.view.frame.width, y: 0))
    path.addLine(to: CGPoint(x: self.view.frame.width, y: self.view.frame.size.height/3))
    path.addCurve(to: CGPoint(x: 0, y: self.view.frame.size.height/3 - 50),
                                controlPoint1: CGPoint(x: 200, y: self.view.frame.size.height/3 - 20),
                                controlPoint2: CGPoint(x: self.view.frame.width/2, y: self.view.frame.size.height/3 - 100))
    path.close()
    
    let shapeLayer = CAShapeLayer()
    shapeLayer.path = path.cgPath
    
    imageView.layer.mask = shapeLayer
    imageView.image = UIImage(named: "spider.jpg")
    self.view.addSubview(imageView)