ios – 使用UIBezierPath和CAShapeLayer时的“无效上下文0x0”

我的困境与 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,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,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,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,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,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,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,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,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,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,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时的“无效上下文0x0”全部内容,希望文章能够帮你解决ios – 使用UIBezierPath和CAShapeLayer时的“无效上下文0x0”所遇到的程序开发问题。

如果觉得来客网网站内容还不错,欢迎将来客网网站推荐给程序员好友。