How to Set Image in Circle in Swift

How to set image in circle in swift

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() {

layer.borderWidth = 1
layer.masksToBounds = false
layer.borderColor = UIColor.black.cgColor
layer.cornerRadius = self.frame.height / 2
clipsToBounds = true
}
}

That is all you need....

How to Set UIImageView in circle Swift 5

As @RajaKishan and @aiwiguna both said that you can't get a square image with both proportional height and width constraint together because then you can not get a round circle with different height and width.

You can set width or height proportional to superview and set the aspect ratio to 1:1 then you can change the multiplier and get the circle properly. You can check to attached image for constraints

Sample Image

then set cornerRadius in viewDidLayoutSubviews(), not in viewDidLoad(). (viewDidLoad() is called before the layout constraints change the view height, and only once. viewDidLayoutSubviews() is called any time your view's geometry changes, so you should invoke any layout logic there.

override func viewDidLayoutSubviews()  {

self.yourImageView.layer.cornerRadius = self.yourImageView.bounds.height/2
self.yourImageView.clipsToBounds = true

}

How to set imageView in circle like imageContacts in Swift correctly?

What frame size are you using for image? I can get a perfect circle if I set the frame to be a square.

let image = UIImageView(frame: CGRectMake(0, 0, 100, 100))

How to make image view round in swift 4

import UIKit

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

func makeRounded() {

image.layer.borderWidth = 1
image.layer.masksToBounds = false
image.layer.borderColor = UIColor.blackColor().CGColor
image.layer.cornerRadius = image.frame.height/2 //This will change with corners of image and height/2 will make this circle shape
image.clipsToBounds = true
}

Happy Coding

Swift Imageview Circular

Your code is making the corner radius half the width. This works fine when height == width (so radius also == height/2), but otherwise it won't work.

To fix this, add constraints to make your profileImageView square, then set the profileImageView.contentMode = .aspectFill.

How to shape an Image into an circle

You were almost there :)

Your only mistake was the order you placed your modifiers. ORDER MATTERS!!

Place scaledToFill() and clipShape() before the frame modifier.

Like such:

.resizable()
.scaledToFill()
.clipShape(Circle())
.frame(width: size, height: size)

Cut a UIImage into a circle

Make sure to import QuarzCore if needed.

 func maskRoundedImage(image: UIImage, radius: CGFloat) -> UIImage {
let imageView: UIImageView = UIImageView(image: image)
let layer = imageView.layer
layer.masksToBounds = true
layer.cornerRadius = radius
UIGraphicsBeginImageContext(imageView.bounds.size)
layer.render(in: UIGraphicsGetCurrentContext()!)
let roundedImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return roundedImage!
}

how to make circular image that is in button in swift?

Set the cornerRadius of button.imageView's to half of button's height in viewDidLayoutSubviews(), i.e.

override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
button.imageEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: button.bounds.width - button.bounds.height)
button.imageView?.layer.cornerRadius = button.bounds.height/2.0
}

I've set the imageEdgeInsets of the button so you can get the exact circular image.

Add a circular image view with corner radius for an image in swiftUI

This is not the simplest thing to come up with. Use this struct as a separate view. It will return the image properly sized on the circle.

struct ImageOnCircle: View {

let icon: String
let radius: CGFloat
let circleColor: Color
let imageColor: Color // Remove this for an image in your assets folder.
var squareSide: CGFloat {
2.0.squareRoot() * radius
}

var body: some View {
ZStack {
Circle()
.fill(circleColor)
.frame(width: radius * 2, height: radius * 2)

// Use this implementation for an SF Symbol
Image(systemName: icon)
.resizable()
.aspectRatio(1.0, contentMode: .fit)
.frame(width: squareSide, height: squareSide)
.foregroundColor(imageColor)

// Use this implementation for an image in your assets folder.
// Image(icon)
// .resizable()
// .aspectRatio(1.0, contentMode: .fit)
// .frame(width: squareSide, height: squareSide)
}
}
}


Related Topics



Leave a reply



Submit