So I have this issue I have been trying to solve for the past few days however, cannot find a solution to the problem. My issue is very simple I am trying to crop a image so that it becomes a circle. From research done, I have found a few methods that can be used to achieve this and the main one which seemed to pop up a lot is to simply crop the UIImageView so that it becomes a circular frame. The code which I used to do this can be seen below.
imageView.layer.masksToBounds = false
imageView.layer.cornerRadius = imageView.frame.height/2
imageView.clipsToBounds = true
The only problem is that this method only crops the frame of the image view and not the image itself. So if I was to save the image to the users camera roll for instance, it would still appear as a rectangle and not a cropped circle. I am a bit lost with how I can actually achieve this as I am relatively new to Xcode and was hoping that someone might be able to provide a method as to how I can crop the image itself not just the frame. Any help on the topic will be greatly appreciated. Thank you.
Lets get on **Graphics Context **.
func makeRoundImg(img: UIImageView) -> UIImage {
let imgLayer = CALayer()
imgLayer.frame = img.bounds
imgLayer.contents = img.image?.cgImage;
imgLayer.masksToBounds = true;
imgLayer.cornerRadius = 28 //img.frame.size.width/2
UIGraphicsBeginImageContext(img.bounds.size)
imgLayer.render(in: UIGraphicsGetCurrentContext()!)
let roundedImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return roundedImage!;
}