Swift绘制圆角图片

在多处地方需要使用圆角图片,方便拿来用

import Foundation

extension UIImage {
    /// 异步设置圆角图片
    ///
    /// - Parameters:
    ///   - size:       图片大小
    ///   - fillColor:  裁切区域填充颜色
    ///   - completion: 回调裁切结果图片
    func Westrice_cornetImage(size:CGSize,fillColor:UIColor = UIColor.white,completion:@escaping (_ image:UIImage?)->()){
        //异步绘制裁剪
        DispatchQueue.global().async {
            //开启上下文
            UIGraphicsBeginImageContextWithOptions(size, true, 0)
            
            let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
            
            //填充颜色
            fillColor.setFill()
            UIRectFill(rect)
            //赛贝尔路径裁剪
            let path = UIBezierPath.init(ovalIn: rect)
            path.addClip()
            self.draw(in: rect)
            
            //获取结果
            let resultImage = UIGraphicsGetImageFromCurrentImageContext()
            //关闭上下文
            UIGraphicsEndImageContext()
            //主队列回调
            DispatchQueue.main.async{
                completion(resultImage)
            }
        }
    }
}

使用方法:

 image?.Westrice_cornetImage(size: (self?.bounds.size)!, fillColor:UIColor.lightGray, completion: { (result:UIImage?) in
                self?.image = result
               })

你可能感兴趣的:(Swift绘制圆角图片)