How to create a black UIImage?

后端 未结 5 1094
无人共我
无人共我 2021-02-08 21:42

I\'ve looked around everywhere, but I can\'t find a way to do this. I need to create a black UIImage of a certain width and height (The width and height change, so I can\'t just

5条回答
  •  孤独总比滥情好
    2021-02-08 22:07

    Swift 3:

    func uiImage(from color:UIColor?, size:CGSize) -> UIImage? {
    
        UIGraphicsBeginImageContextWithOptions(size, true, 0)
        defer {
            UIGraphicsEndImageContext()
        }
    
        let context = UIGraphicsGetCurrentContext()
        color?.setFill()
        context?.fill(CGRect.init(x: 0, y: 0, width: size.width, height: size.height))
        return UIGraphicsGetImageFromCurrentImageContext()
    }
    

提交回复
热议问题