iosswiftswift3uicolorcgsize

add uiColor for cgsize (swift3)


Right now newSize just creates a white box. I would like newSize to be a blue box. How do I add uiColor to newSize?

          let newSize: CGSize = CGSize(width: 900, height: 900)
        UIGraphicsBeginImageContext(newSize)

Solution

  • You need to use your color setFill() method to define the color that you would like to fill your image context and just use a UIBezierPath to fill the desired area:

    let newSize = CGSize(width: 900, height: 900)
    UIGraphicsBeginImageContextWithOptions(newSize, false, 0)
    defer { UIGraphicsEndImageContext() } 
    
    UIColor.blue.setFill()
    UIBezierPath(rect: CGRect(origin: .zero, size: newSize)).fill()
    
    UIGraphicsGetImageFromCurrentImageContext()