填充UIBezlerPath时“无效上下文0x0”

时间:2016-09-05 20:46:55

标签: ios objective-c uibezierpath

怎么办? 当我设置Backtracing Variable时,我收到了这个错误。

此代码确实导致错误:

UIBezierPath* ovalPath = [UIBezierPath bezierPath];
[ovalPath addArcWithCenter: CGPointMake(CGRectGetMidX(ovalRect), CGRectGetMidY(ovalRect)) radius: CGRectGetWidth(ovalRect) / 2 startAngle: 180 * M_PI/180 endAngle: 0 * M_PI/180 clockwise: YES];
[ovalPath addLineToPoint: CGPointMake(CGRectGetMidX(ovalRect), CGRectGetMidY(ovalRect))];
[ovalPath closePath];

[UIColor.grayColor setFill];
[ovalPath fill];

错误:

CGContextSetFlatness: invalid context 0x0. Backtrace:
  <<redacted>+48>
   <-[ViewController viewDidLoad]+2868>
    <<redacted>+996>
     <<redacted>+28>
      <<redacted>+76>
       <<redacted>+252>
        <<redacted>+48>
         <<redacted>+3456>
          <<redacted>+1684>
           <<redacted>+168>
            <<redacted>+36>
             <<redacted>+168>
              <<redacted>+56>
               <<redacted>+24>
                <<redacted>+540>
                 <<redacted>+724>
                  <CFRunLoopRunSpecific+384>
                   <<redacted>+460>
                    <UIApplicationMain+204>
                     <main+124>
Sep  5 21:55:56  Motion Tracking Radar - Aliem[2934] <Error>: 

1 个答案:

答案 0 :(得分:3)

[ovalPath fill]只有在当前有全局CGContext时才会被调用(即调用UIGraphicsGetCurrentContext()会返回nil以外的内容。获得此功能的最常见方法是在drawRect:内运行,但您可以根据需要创建临时CGContext对象,通常使用CGBitmapContextCreate,并使其成为当前上下文UIGraphicsPushContext 1}}。

在大多数情况下,当人们遇到这个问题时,这是因为他们相信他们可以在程序中的随机点上绘制到屏幕上。这不是真的。您只能在绘制周期中绘制,这通常是通过将代码放在视图drawRect:中来实现的。 (还有其他选项使用图层等,但这是最常用的方法。)

有关Cocoa绘图的介绍,请参阅Drawing and Printing Guide for iOS(或Mac上的等效文件)。