swift把颜色转成图片

// 把颜色转成图片 

 

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

        let rect: CGRect = CGRect(x: 0, y: 0, width: 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(width: kScreenW, height: 1)), for: UIBarPosition.any, barMetrics: UIBarMetrics.default)

        

        navigationController?.navigationBar.shadowImage = UIImage()

posted @ 2017-04-21 14:21  super1250  阅读(1691)  评论(0编辑  收藏  举报