4

I have tried every answer variation I have found and all give me the same result. A weird almost diamond shaped image view. I was using this:

imageView.layer.cornerRadius = imageView.frame.width/2 
imageView.clipsToBounds = true

This worked with a previous project I was working on but now when I try it I get the weird diamond.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
01Riv
  • 1,444
  • 1
  • 13
  • 28
  • 1
    Does ur imageview have same height and width – Moin Shirazi Apr 10 '16 at 04:25
  • Yeah I've made it a square every time – 01Riv Apr 10 '16 at 04:29
  • Your image view's frame is probably changing after you set the `cornerRadius`. This is actually the wrong way to create a circular image. You should actually create a new `UIImage` from the original cropped to be round. It's much better than setting the `cornerRadius` of the image view. – rmaddy Apr 10 '16 at 04:39
  • That does sound like a much better solution. Can you explain to me how to do this? Thanks! – 01Riv Apr 10 '16 at 04:42

6 Answers6

7

Here is the solution how to create UIImageView Circular. This Approach is new

  1. Create a new Designable.swift file in your project.
  2. Copy the following code in your Designable.swift file.

    import UIKit
    
    @IBDesignable class DesignableImageView: UIImageView { }
    @IBDesignable class DesignableButton:UIButton { }
    @IBDesignable class DesignableTextField:UITextField { }
    
    extension UIView {
       @IBInspectable
       var borderWidth :CGFloat {
       get {
           return layer.borderWidth
       }
    
       set(newBorderWidth){
          layer.borderWidth = newBorderWidth
       }
    }
    
    @IBInspectable
    var borderColor: UIColor? {
       get{
           return layer.borderColor != nil ? UIColor(CGColor: layer.borderColor!) :nil
       }
       set {
           layer.borderColor = newValue?.CGColor
       }
    }
    
    @IBInspectable
    var cornerRadius :CGFloat {
        get {
            return layer.cornerRadius
        }
    
        set{
           layer.cornerRadius = newValue
           layer.masksToBounds = newValue != 0
        }
    }
    
    @IBInspectable
    var makeCircular:Bool? {
        get{
            return nil
        }
    
        set {
            if let makeCircular = newValue where makeCircular {
                cornerRadius = min(bounds.width, bounds.height) / 2.0
            }
        }
      }
    }
    
  3. Now after this select your ImageView on StoryBoard and Select Identity Inspector from Utilities Panel.

  4. In Custom Class section select the custom class from the drop-down menu naming DesignableImageView and hit return. You will see the designable update after hitting return.

  5. Now go to attribute inspector in utility panel and you can add the desired Corner Radius for the image to be circular.

P.S. If your image is rectangle this will try to make it square and then best possible circle.

Attached images for reference.

Select Custom class

Set Attributes

vignan kumar
  • 1
  • 1
  • 1
  • 3
niks290192
  • 694
  • 1
  • 9
  • 23
1

Try changing the view mode for the uiimageview. It might have been set to Aspect fit making it to look like a diamond.

Or you can create a circular uiimage using the code below,

imageLayer.contents = yourImage.CGImage
let mask = CAShapeLayer()
let dx = lineWidth + 1.0
let path = UIBezierPath(ovalInRect: CGRectInset(self.bounds, dx, dx))
mask.fillColor = UIColor.blackColor().CGColor
mask.path = path.CGPath
mask.frame = self.bounds
layer.addSublayer(mask)

imageLayer = CAShapeLayer()
imageLayer.frame = self.bounds
imageLayer.mask = mask
imageLayer.contentsGravity = kCAGravityResizeAspectFill
layer.addSublayer(imageLayer)
Rjk
  • 1,356
  • 3
  • 15
  • 25
1

If you want to create with new image it may be like this

let img = UIImage(named: "logo.png")

UIGraphicsBeginImageContextWithOptions(imgView.bounds.size, false, 0.0);
// Add a clip before drawing anything, in the shape of an rounded rect

UIBezierPath(roundedRect: imgView.bounds, cornerRadius:imgView.bounds.size.width/2).addClip()
img!.drawInRect(imgView.bounds)
imgView.image = UIGraphicsGetImageFromCurrentImageContext()

UIGraphicsEndImageContext();

Ref & If you want to make extension for it : https://stackoverflow.com/a/25459500/4557505
You can find more information in the above link for other alternative solutions

Community
  • 1
  • 1
HardikDG
  • 5,892
  • 2
  • 26
  • 55
0

Your image view should be a square in order for this to work.

rounak
  • 9,217
  • 3
  • 42
  • 59
0

if you are trying this code in viewDidLoad(), please try it in viewDidLayoutSubviews()

viewDidLayoutSubviews() {

imageView.layer.cornerRadius = imageView.frame.size.width/2
imageView.layer.masksToBounds = true
}
0
extension UIImageView {
  public func maskCircle(inputImage: UIImage) {
    self.contentMode = UIViewContentMode.scaleAspectFill
    self.layer.cornerRadius = self.frame.height / 2
    self.layer.masksToBounds = false
    self.clipsToBounds = true

   self.image = inputImage
  }
}

Usage example of the above extension:

let yourImage:UIImage = UIImage(named: "avatar")!
yourImageView.maskCircle(yourImage)
JipZipJib
  • 81
  • 3
  • 5