0

I'm making an app that needs to access users image gallery and when he chooses an image for his profile image should become rounded. Here is the screenshot of the problem: enter image description here

and a sample code used in the project

@objc func makeImageRounded(image: UIImageView){
        image.layer.cornerRadius = image.frame.size.width / 2
        image.clipsToBounds = true
    }

this function is called in

   imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]){
            self.perform(#selector(makeImageRounded(image:)), with: profilePicture, afterDelay: 0)

    }

4 Answers4

1

Try , with imageView aspect ratio = 1

func viewDidLayoutSubviews
{
    image.layer.cornerRadius = image.frame.size.width / 2
    image.clipsToBounds = true

}

and call

self.view.layoutIfNeeded()
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
1

Your ImageView should have Equal widths and heights [200*200 / 100*100]

Set ImageView contenMode, ScaleToFill / AspectFill / AspectFit

image.backGroundcolor = UIColor.red // To check imageView is in Circular or not
image.layer.cornerRadius = image.frame.size.width / 2 // To get Rounded Corner

image.clipsToBounds = true // To Trim Outer frame
McDonal_11
  • 3,935
  • 6
  • 24
  • 55
  • 2
    Using backgroundcolor showed me that my content mode was bad i changed content mode to Aspect Fill and that managed to fix the issue – Dusan Dimic Feb 12 '18 at 07:48
1

Your imageView is not rounded ?

try

image.layer.cornerRadius = image.frame.size.width / 2
image.clipsToBounds = true
self.view.layoutIfNeeded()

if it is still not working it is issue of content mode. you can check same with setting background colour of your imageView

if you set your content mode to aspectFill or scaleToFill then you can see image will be filled to bounds of it.

Prashant Tukadiya
  • 15,838
  • 4
  • 62
  • 98
0
imageView.layer.cornerRadius = image.frame.size.height / 2
imageView.clipsToBounds = true
self.view.layoutIfNeeded()
Pratyush Pratik
  • 683
  • 7
  • 14