【问题标题】:Invalid Content error while drawing a UIBezierPath绘制 UIBezierPath 时出现无效内容错误
【发布时间】:2014-03-01 20:55:24
【问题描述】:

我正在尝试使用以下代码在 viewdidload 中创建一个简单的圆圈:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    UIBezierPath* aPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(100,100) radius:100 startAngle:M_PI/6 endAngle:M_PI clockwise:YES];


    [aPath fill];
}

我收到以下错误:

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.

我知道这个问题已经在讨论here,但我无法理解。错误发生在

[aPath fill];

这与应用程序生命周期有关吗?

【问题讨论】:

  • 除了记录此错误之外,您的程序是否正常运行?
  • 它显示一个空白视图。没有圈。应用程序不会崩溃。

标签: ios cgcontext uibezierpath


【解决方案1】:

这个错误意味着你没有上下文可以绘制路径,你必须有一个。

由于此代码位于视图控制器中,因此您需要决定应该绘制到哪个上下文中。

您可以自己创建一个上下文并在其中绘制路径,这将是有益的,例如,如果您想创建包含路径的图像。这将使用CGBitmapContextCreate 完成。

或者,可能更多您正在寻找的东西,通过将路径绘制到视图控制器视图上下文中。这在drawRect: 方法中可用。因此,您可以在自定义视图子类中实现它并将代码移到那里。

另一种方法是使用CAShapeLayer,使用路径创建(使用CGPath)并将其添加为视图控制器视图层的子层。那么你根本不需要担心上下文......

【讨论】:

  • 谢谢。添加了一个自定义 UIView 并将这些行添加到 drawrect 方法中。可以使用主视图的drawrect吗?
  • 如果您制作自定义类的主视图,可以(在 XIB/storyboard/loadView 方法中)
猜你喜欢
  • 2020-04-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-16
  • 1970-01-01
相关资源
最近更新 更多