1

From the below code I got only oval shaped image I don't know why and what I did wrong..?

   self.viewCirlce.layer.cornerRadius = self.viewCirlce.frame.size.width / 2
   self.viewCirlce.clipsToBounds = true
imac apple
  • 53
  • 1
  • 9
  • 1
    Does your `viewCirlce ` height equal to width? – Tj3n Mar 27 '17 at 07:39
  • please do some google you will get similar question over stackoverflow. http://stackoverflow.com/questions/25587713/how-to-set-imageview-in-circle-like-imagecontacts-in-swift-correctly – Pankaj K. Mar 27 '17 at 08:09

5 Answers5

5

This code used to display the circle image

image.layer.borderWidth = 1
image.layer.masksToBounds = false
image.layer.borderColor = UIColor.blackColor().CGColor
image.layer.cornerRadius = image.frame.height/2
image.clipsToBounds = true
Vignesh J
  • 257
  • 1
  • 2
  • 14
  • Otherwise put a your imageview Height and Width are equal Size like let image = UIImageView(frame: CGRectMake(0, 0, 100, 100)) – Vignesh J Mar 27 '17 at 08:04
3

My guess would be that the component you have called viewCirlce is a rectangle to start with, you are just setting the corner radius. If the component has the same width and height then this could give you a circle. If it's a rectangle, then you'll get an ellipse.

Dave Durbin
  • 3,562
  • 23
  • 33
1
import UIKit

class ViewController: UIViewController {
  @IBOutlet weak var image: UIImageView!

  override func viewDidLoad() {
    super.viewDidLoad()

    image.layer.borderWidth = 1
    image.layer.masksToBounds = false
    image.layer.borderColor = UIColor.blackColor().CGColor
    image.layer.cornerRadius = image.frame.height/2
    image.clipsToBounds = true
}
Nikit Barochiya
  • 971
  • 11
  • 14
0

add this code and make sure your viewCirlce is a square view

 self.viewCirlce.layer.masksToBounds = true  
Henson Fang
  • 1,177
  • 7
  • 30
0

Make sure height and width of viewCirlce are equal and put your this code in a method which is called after viewCirlce is loaded for e.g :

override func viewDidLayoutSubviews() {
      self.viewCirlce =  self.viewCirlce.frame.size.height / 2.0
      self.viewCirlce.layer.masksToBounds = true
}
Jagdeep Singh
  • 2,556
  • 3
  • 18
  • 28
  • I have the same height and width for the viewCircle but the image displayed in oval shape.... – imac apple Mar 27 '17 at 09:48
  • i have edit my answer to explain in more detail please check it. Just copy from here and past in your code and remove code you are using to round the view. And please me know if any issue. – Jagdeep Singh Mar 27 '17 at 09:59