Cut a UIImage into a circle

后端 未结 11 2289
面向向阳花
面向向阳花 2020-11-27 13:30

I want to cut a UIImage into a circle so that I can then use it as an annotation. Every answer on this site that I\'ve found describes creating an UIImage

11条回答
  •  庸人自扰
    2020-11-27 13:45

    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!
        }
    

提交回复
热议问题