Search code examples
iosswiftcore-graphicsuibezierpathmkoverlay

UIBezierPath set context


I'm trying to draw some lines using UIBezierPath in an overlay renderer. I get error CGContextSetStrokeColorWithColor: invalid context 0x0 which I suspect is due to the UIBezierPath not knowing the context. How do I set the UIBezierPath's context?

My MKOverlayRenderer is roughly:

override func drawMapRect(mapRect: MKMapRect, zoomScale: MKZoomScale, inContext context: CGContext) {
    let path = UIBezierPath()

    path.moveToPoint(...)
    path.addLineToPoint(...)

    path.lineWidth = 2
    UIColor.blueColor().setStroke()
    path.stroke()
}

Solution

  • You need to directly operate on the context, not try to teach the path about the context. So you'd use CGContextAddPath and related functions.