获得"无效的背景" UIBezierPath出错

时间:2015-04-02 21:48:33

标签: swift uibezierpath

我试图让这个画出一条平移手势发生的线。问题是当我尝试使用此代码时

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
    }
}

...我收到多个错误,我不太清楚该怎么做。

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.

1 个答案:

答案 0 :(得分:17)

这告诉您,您正在尝试在有效上下文(CGContextRef)之外调用CoreGraphics函数。除非您在stroke drawRect子类或UIViewUIGraphicsBeginImageContextWithOptions之间(或类似内容)之间进行操作,否则不应致电UIGraphicsEndImageContext。但是,如果您不在图形上下文中,则不能只stroke路径。

如果您只想将此路径添加到UIView,还可以创建CAShapeLayer,并使用此贝塞尔路径的cgPath,并获取结果{{1}并将其添加为视图图层的子图层。例如,在Swift 3及更高版本中:

CAShapeLayer