ios - 使用 UIBezierPath 和 CAShapeLayer 时为 "invalid context 0x0"

标签 ios objective-c uibezierpath cashapelayer

我遇到的困境与 this SO thread 中描述的困境非常相似.我通读了该线程上的每个答案,但找不到任何解决我问题的答案。我已将我的问题缩小到下面函数内的 4 行。 4 行中的每一行都输出几行错误,所有错误都列在下面(我已删除重复项)。

我尝试将 [path closePath]; 移动到这 4 行下方,但它没有任何改变。我还在第一行之前设置了一个断点,并逐行手动执行该函数,而正是这 4 行造成了严重破坏。

这个问题似乎是一种趋势,所有内容都按应有的方式呈现,但它会用这些类型的消息淹没控制台。

非常感谢任何帮助,我很乐意提供更多信息和更新。


功能:

-(CAShapeLayer *)lineBetweenPoint:(CGPoint)start andPoint:(CGPoint)end {

    //Draw the first circle
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:start radius:kLineEndRadius startAngle:0 endAngle:DEGREES_TO_RADIANS(360) clockwise:TRUE];

    //Add the line
    [path moveToPoint:start];
    [path addLineToPoint:end];
    [path moveToPoint:end];

    //Draw the second circle
    [path addArcWithCenter:end radius:kLineEndRadius startAngle:0 endAngle:DEGREES_TO_RADIANS(360) clockwise:TRUE];

    //Close the path and set the coloring
    [path closePath];

    /*The following 4 lines cause problems*/
    [[UIColor blueColor] setStroke]; /*CAUSES PROBLEM*/
    [[UIColor blueColor] setFill]; /*CAUSES PROBLEM*/
    [path stroke]; /*CAUSES PROBLEM*/
    [path fill]; /*CAUSES PROBLEM*/

    //Create a shape layer
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.path = [path CGPath];
    shapeLayer.strokeColor = [kLineColor CGColor];
    shapeLayer.lineWidth = kLineWidth;
    shapeLayer.fillColor = [kLineColor CGColor];

    //Return the layer
    return shapeLayer;
}

日志输出:

: CGContextSetStrokeColorWithColor: 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.

: CGContextSetFillColorWithColor: 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.

: 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.

: 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.

: 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.

: 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.

: 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.

: 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.

: 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.

: 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.

: 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.

最佳答案

您正在混合 API。当您使用 CAShapeLayer 时,您实际上并不自己进行绘图,您只需配置对象并让 CoreAnimation 渲染服务器进行绘图。使用 CoreGraphics,您可以在 CGContextRef 中进行绘图。您的问题是需要创建 CGContextRef(在 -[UIView drawRect:] 中自动发生)。您会使用 UIGraphicsBeginImageContext 之一来执行此操作……通常,但在这里您实际上不需要 CG,您需要 CA。因此,只需省略这四行并确保您的 CAShapeLayer 已正确配置,就可以了。

关于ios - 使用 UIBezierPath 和 CAShapeLayer 时为 "invalid context 0x0",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25099469/

相关文章:

ios - 我希望将导航栏中的栏按钮项放置在导航栏的最角落

objective-c - 如何在 iOS 上定义可达性超时

iphone - 如何创建可变大小的 UITableViewCell?

ios - ovalInRect 中的起点

objective-c - 请设置 CG_CONTEXT_SHOW_BACKTRACE 环境变量

ios - 1个UITextField上有2个子类

ios - 我想用 IOS SpriteKit 制作一个足球

ios - NSAttributedString 仅适用于 Helvetica Neue

ios - 我的 UIBezierPath 出了什么问题?

ios - WKWebView 中的 evaluateJavaScript() 仅在第一次运行