Search code examples
swiftuibezierpath

Getting "invalid context" error with UIBezierPath


I am trying to make this draw a line where the pan gesture happens. The problem is when I try to use this code

var firstPoint: CGPoint!

var path: UIBezierPath!

@IBAction func panned(gesture: UIPanGestureRecognizer) {
    switch gesture.state {
    case .Began: firstPoint = gesture.locationOfTouch(0, inView: view)
    case .Ended: gesture.cancelsTouchesInView = true
    case .Changed:
        path.moveToPoint(firstPoint)
        path.addLineToPoint(gesture.translationInView(view))
        path.stroke()
    default: break
    }
}

...I get multiple errors which I am not quite sure what to do about.

Apr 2 17:40:03 MacBook-Pro.local Paint[6729] <Error>: CGContextSaveGState: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context  and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.
Apr 2 17:40:03 MacBook-Pro.local Paint[6729] <Error>: CGContextSetLineWidth: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context  and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.
Apr 2 17:40:03 MacBook-Pro.local Paint[6729] <Error>: CGContextSetLineJoin: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context  and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.
Apr 2 17:40:03 MacBook-Pro.local Paint[6729] <Error>: CGContextSetLineCap: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context  and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.
Apr 2 17:40:03 MacBook-Pro.local Paint[6729] <Error>: CGContextSetMiterLimit: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context  and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.
Apr 2 17:40:03 MacBook-Pro.local Paint[6729] <Error>: CGContextSetFlatness: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context  and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.
Apr 2 17:40:03 MacBook-Pro.local Paint[6729] <Error>: CGContextAddPath: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context  and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.
Apr 2 17:40:03 MacBook-Pro.local Paint[6729] <Error>: CGContextDrawPath: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context  and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.
Apr 2 17:40:03 MacBook-Pro.local Paint[6729] <Error>: CGContextRestoreGState: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context  and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.

Solution

  • This is telling you that you are trying to call CoreGraphics functions outside of a valid context (a CGContextRef). You should not call stroke unless you're doing it within the drawRect of UIView subclass or between UIGraphicsBeginImageContextWithOptions and UIGraphicsEndImageContext (or something equivalent). But you cannot just stroke a path if you are not within a graphics context.

    If you just want to add this path to a UIView, you can also create a CAShapeLayer, and use the cgPath of this bezier path, and take the resulting CAShapeLayer and add it as a sublayer of the view's layer. For example, in Swift 3 and later:

    var drawPath: UIBezierPath!
    var shapeLayer: CAShapeLayer!
    
    func handlePan(gesture: UIPanGestureRecognizer) {
        let location = gesture.location(in: gesture.view)
    
        if gesture.state == .began {
            drawPath = UIBezierPath()
            drawPath.move(to: location)
    
            shapeLayer = CAShapeLayer()
            shapeLayer.strokeColor = UIColor.black.cgColor
            shapeLayer.fillColor = UIColor.clear.cgColor
            shapeLayer.lineWidth = 4.0
            gesture.view?.layer.addSublayer(shapeLayer)
        } else if gesture.state == .changed {
            drawPath.addLine(to: location)
            shapeLayer.path = drawPath.cgPath
        }
    }