swift把颜色转成图片

// 把颜色转成图片 

 

 func imageFromColor(color: UIColor, viewSize: CGSize) -> UIImage{

        let rect: CGRect = CGRect(x: 0, y: 0, viewSize.width, height: viewSize.height)

        UIGraphicsBeginImageContext(rect.size)

        let context: CGContext = UIGraphicsGetCurrentContext()!

        context.setFillColor(color.cgColor)

        context.fill(rect)

        

        let image = UIGraphicsGetImageFromCurrentImageContext()

        UIGraphicsGetCurrentContext()

        return image!

    }

 

 

// 调用方法替换导航栏背景色

        navigationController?.navigationBar.setBackgroundImage(imageFromColor(color: UIColor.white.withAlphaComponent(0), viewSize: CGSize( kScreenW, height: 1)), for: UIBarPosition.any, barMetrics: UIBarMetrics.default)

        

        navigationController?.navigationBar.shadowImage = UIImage()

原文地址:https://www.cnblogs.com/supersr/p/6743410.html