0

I have used the following method to make the image rounded, but the results are not the desired ones. I have shared my code and the result in the description below. Any suggestions would be highly appreciated. Thanks!

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var image1: UIImageView!

override func viewDidLoad() {
    super.viewDidLoad()
    
    DispatchQueue.main.async(execute:{self.image1.setRounded()})
    
    
    // Do any additional setup after loading the view.
}

extension UIImageView {

func setRounded() {
    let radius = self.frame.width / 2
    self.layer.cornerRadius = radius
    self.layer.masksToBounds = true
    }

}

Results :-

enter image description here

Pressing_Keys_24_7
  • 1,755
  • 2
  • 7
  • 33

1 Answers1

1

To make the image rounded, you have to make the height equal to the width. And then try:

extension UIImageView {
    func setRounded() {
        layer.cornerRadius = bounds.height/2
        layer.masksToBounds = true
    }
}

And call it inside viewDidLayoutSubviews.

override func viewDidLayouSubviews() {
   super.viewDidLayouSubviews()
   image1.setRounded()
}
Frankenstein
  • 15,732
  • 4
  • 22
  • 47