Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set image in circle in swift

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/

like image 459
Pixel Avatar asked Jan 21 '15 18:01

Pixel


People also ask

How do I make an image circular in Swift?

Implementing the Round Image Section To do this, first add the viewWillAppear method inside the viewcontroller. swift file as shown below. after doing it, we will first call the image with its name and then later we can set the corner radius and also the cliptobound to true to make the round image as shown below.

What is image view in iOS?

ImageView can be defined as an object that can display the images on the interface of the iOS applications. It is the instance of the UIImageView class, which inherits UIView.


2 Answers

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.black.cgColor     image.layer.cornerRadius = image.frame.height/2     image.clipsToBounds = true } 

If you want it on an extension

import UIKit  extension UIImageView {      func makeRounded() {          self.layer.borderWidth = 1         self.layer.masksToBounds = false         self.layer.borderColor = UIColor.black.cgColor         self.layer.cornerRadius = self.frame.height / 2         self.clipsToBounds = true     } } 

That is all you need....

like image 85
Jon Avatar answered Oct 13 '22 06:10

Jon


You can simple create extension:

import UIKit  extension UIImageView {     func setRounded() {       let radius = CGRectGetWidth(self.frame) / 2       self.layer.cornerRadius = radius       self.layer.masksToBounds = true    } } 

and use it as below:

imageView.setRounded() 
like image 43
Daniel Kuta Avatar answered Oct 13 '22 06:10

Daniel Kuta