How can i make i circle picture with swift ?
My ViewController :
import UIKit
import Foundation
class FriendsViewController : UIViewController{
@IBOutlet weak var profilPicture: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
profilPicture = UIImageView(frame: CGRectMake(0, 0, 100, 100))
}
}
My profilPicture = UIImageView(frame: CGRectMake(0, 0, 100, 100))
do nothing ..
Exemple: http://www.appcoda.com/ios-programming-circular-image-calayer/
Based in the answer of @DanielQ
Swift 4 and Swift 3
You can use it in any
ViewController
with:For Swift 4:
If your image is rounded, it would have a height and width of the exact same size (i.e 120). You simply take half of that number and use that in your code (image.layer.cornerRadius = 60).
First you need to set equal width and height for getting Circular ImageView.Below I set width and height as 100,100.If you want to set equal width and height according to your required size,set here.
Then you need to set height/2 for corner radius
This way is the least expensive way and always keeps your image view rounded:
The other answers are telling you to make views rounded based on frame calculations set in a
UIViewController
sviewDidLoad()
method. This isn't correct, since it isn't sure what the final frame will be.All the answers above couldn't solve the problem in my case. My
ImageView
was placed in a customizedUITableViewCell
. Therefore I had also call thelayoutIfNeeded()
method from here. Example: