iOS Swift3.0异步设置任意弧度高性能圆角图片

写一个UIImage的分类,方便调用

import UIKit

extension UIImage {

    /// 异步设置圆角图片
    ///
    /// - Parameters:
    ///   - size:       图片大小
    ///   - radius:     圆角值
    ///   - fillColor:  裁切区域填充颜色
    ///   - completion: 回调裁切结果图片
    func cornerImage(size:CGSize, radius:CGFloat, fillColor: UIColor, completion:@escaping ((_ image: UIImage)->())) -> Void {

        //异步绘制裁切
        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(roundedRect: rect, cornerRadius: radius)
            path.addClip()

            self.draw(in: rect)

            //获取结果
            let resultImage = UIGraphicsGetImageFromCurrentImageContext()

            //关闭上下文
            UIGraphicsEndImageContext()

            //主队列回调
            DispatchQueue.main.async {
                completion(resultImage!)
            }
        }
    }
}

调用绘制方法

let image = UIImage(named: "imageName")
image?.cornerImage(size: CGSize(width: 200, height: 200), radius:30, fillColor: UIColor.white) { (resultImage: UIImage) in
    self.imageView.image = resultImage
}

你可能感兴趣的:(Swift,iOS,iOS_Swift3.0)