iOS Swift Crop Image to Circle or Rounded Corners example

iOS Swift Crop Image to Circle or Rounded Corners example

import UIKit

class ViewController: UIViewController {
    
    let myCroppedImageView = UIImageView()
    let myCroppedImageView2 = UIImageView()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        print("viewDidLoad")
        self.navigationItem.title = "Main View"
        
        
        let myImageView = UIImageView()
        myImageView.image = UIImage(named: "switzerland.jpg")
        myImageView.contentMode = .scaleAspectFit
        myImageView.translatesAutoresizingMaskIntoConstraints = false
        self.view.addSubview(myImageView)
        
        myCroppedImageView.image = UIImage(named: "switzerland.jpg")
        myCroppedImageView.contentMode = .scaleAspectFit
        myCroppedImageView.translatesAutoresizingMaskIntoConstraints = false
        self.view.addSubview(myCroppedImageView)
        
        myCroppedImageView2.image = UIImage(named: "switzerland.jpg")
        myCroppedImageView2.contentMode = .scaleAspectFit
        myCroppedImageView2.translatesAutoresizingMaskIntoConstraints = false
        self.view.addSubview(myCroppedImageView2)
        
        var allConstraints: [NSLayoutConstraint] = []
        let views = ["view": view!, "image": myImageView, "image1": myCroppedImageView,
                     "image2": myCroppedImageView2]
        
        let horizontalConstraints = NSLayoutConstraint.constraints(
            withVisualFormat: "H:|-[image(<=300)]-(>=20)-[image1(<=300)]-(>=20)-[image2(<=300)]-|", metrics: nil, views: views)
        allConstraints += horizontalConstraints
        
        var verticalConstraints = NSLayoutConstraint.constraints(
            withVisualFormat: "V:|-(>=40)-[image(<=300)]", metrics: nil, views: views)
        allConstraints += verticalConstraints
        verticalConstraints = NSLayoutConstraint.constraints(
            withVisualFormat: "V:|-(>=40)-[image1(<=300)]", metrics: nil, views: views)
        allConstraints += verticalConstraints
        verticalConstraints = NSLayoutConstraint.constraints(
            withVisualFormat: "V:|-(>=40)-[image2(<=300)]", metrics: nil, views: views)
        allConstraints += verticalConstraints
        
        NSLayoutConstraint.activate(allConstraints)
        
        
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        
        myCroppedImageView.layer.cornerRadius = myCroppedImageView.frame.size.width/2
        myCroppedImageView.layer.masksToBounds = true
        
        myCroppedImageView2.layer.cornerRadius = 50
        myCroppedImageView2.layer.masksToBounds = true
        
    }
    
    
}