将UIImage切成圆形 [英] Cut a UIImage into a circle

查看:105
本文介绍了将UIImage切成圆形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将 UIImage 剪切成一个圆圈,以便我可以将其用作注释。我发现这个网站上的每个答案都描述了创建 UIImageView ,然后修改并显示它,但你不能将注释的图像设置为 UIImageView ,只有 UIImage
我应该怎么做?

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 UIImageView, then modifying that and displaying it, but you cant set the image of an annotation to an UIImageView, only a UIImage. How should I go about this?

推荐答案

如果需要,请务必导入QuarzCore。

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

这篇关于将UIImage切成圆形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆