I came across an example for using UIGraphicsImageRender
to draw in a tutor on Medium. The example code is as follows:
let renderer = UIGraphicsImageRenderer(size: CGSize(width: 20, height: 20))
let img = renderer.image { (ctx) in
let size = renderer.format.bounds.size
UIColor.red.setFill()
ctx.fill(CGRect(x: 0, y: 0, width: size.width, height: size.height))
}
In the above code, UIColor.red.setFill()
apparently only makes the specified colour, i.e. red, ready to fill up a certain shape. However, magically, the ctx, which is a UIGraphicsImageRendererContext
, seems to have received a notification that it will fill up the CGRect
shape with the red colour!
Purely from the code here, I cannot see the connection between the UIColor
instance method setFill()
and the UIGraphicsImageRendererContext
instance method fill(_: CGRect)
. So, how does it know? How does UIGraphicsImageRendererContext
fills when UIColor
sets fill?
Thank you.