Search code examples
iosswiftuiimageviewuiimageswift4

How to make image view round in swift 4


enter image description hereThough there is solution of this question is present in internet, but I am unable to do so, I want to round a image.

this code I am using:

extension UIImageView {        
    func makeRounded() {
        let radius = self.frame.width/2.0
        self.layer.cornerRadius = radius
        self.layer.masksToBounds = true
    }
 }

then i call this function in viewdidload() like imgvw.makeRounded(). but it is not coming. please help

the previous link is not helping me


Solution

  • import UIKit
    
    class ViewController: UIViewController {
      @IBOutlet weak var image: UIImageView!
    
      func makeRounded() {
    
        image.layer.borderWidth = 1
        image.layer.masksToBounds = false
        image.layer.borderColor = UIColor.blackColor().CGColor
        image.layer.cornerRadius = image.frame.height/2 //This will change with corners of image and height/2 will make this circle shape
        image.clipsToBounds = true
    }
    

    Happy Coding