How to clip canvas context in PKCanvasView

Sometimes I need restrict the painting area in a PKCanvasView. I have tried mask the layer of PKCanvasView and it work fine.

Code Block Swift
let mask = CAShapeLayer()
mask.path = cgPath
canvasView.layer.mask = mask


But sometimes it is difficult to get the cgPath. I get the rect and maskImage by use floodfill, how to clip canvas context in PKCanvasView?

Code Block Swift
context.clip(to: rect, mask: maskImage)


How to clip canvas context in PKCanvasView
 
 
Q