Search code examples
iosswiftimageuiimagepickercontrollerrounded-corners

Swift Circle Image using image picker controller


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)

    }

Solution

  • 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