尝试使用 UIBezierPath 绘制时收到错误

发布于 2024-09-26 07:39:41 字数 1329 浏览 1 评论 0原文

我正在尝试在自定义 UIView 中画一些线。

据我所知,为了节省 CoreGraphics 的麻烦,我可以使用 UIBezierPath (我在 Mac 上对 NSBezierPath 做了类似的事情)。 我有一些代码试图绘制线条,但出现输出错误,并且找不到一些示例代码的合适参考来说明发生了什么,有什么想法吗? 下面的代码...

代码:

  - (void)drawRect:(CGRect)rect {
    // Drawing code
    UIBezierPath *line1 = [UIBezierPath bezierPath];
 [[UIColor blackColor] setStroke];
 [line1 setLineWidth:3];
 [line1 moveToPoint:CGPointMake(0, 0)];
 [line1 addLineToPoint:CGPointMake(320, 480)];
 [line1 stroke];

}

错误:

Sat Oct  2 19:26:43 mercury.config mobileManual[46994] <Error>: CGContextSetStrokeColorWithColor: invalid context 0x0

更新:这是当前的代码,没有错误但也没有绘图..想法?

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor yellowColor]];
    [self.view setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect {
    // Drawing code
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSaveGState(context);
    UIBezierPath *line1 = [UIBezierPath bezierPath];
    [line1 setLineWidth:3];
    [line1 moveToPoint:CGPointMake(0, 0)];
    [line1 addLineToPoint:CGPointMake(320, 480)];
    [line1 stroke];
    CGContextRestoreGState(context);
}

I'm trying to draw some lines inside my custom UIView.

From what I can see, to save messing around with CoreGraphics, I can use UIBezierPath (i've done similar with NSBezierPath on the Mac).
I have some code that tries to draw the lines but I get output errors and can't find a decent reference with some sample code to illustrate whats going on, any ideas?
Code below...

Code:

  - (void)drawRect:(CGRect)rect {
    // Drawing code
    UIBezierPath *line1 = [UIBezierPath bezierPath];
 [[UIColor blackColor] setStroke];
 [line1 setLineWidth:3];
 [line1 moveToPoint:CGPointMake(0, 0)];
 [line1 addLineToPoint:CGPointMake(320, 480)];
 [line1 stroke];

}

Errors:

Sat Oct  2 19:26:43 mercury.config mobileManual[46994] <Error>: CGContextSetStrokeColorWithColor: invalid context 0x0

UPDATE: Here's the current code, No errors but also no drawing.. ideas?

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor yellowColor]];
    [self.view setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect {
    // Drawing code
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSaveGState(context);
    UIBezierPath *line1 = [UIBezierPath bezierPath];
    [line1 setLineWidth:3];
    [line1 moveToPoint:CGPointMake(0, 0)];
    [line1 addLineToPoint:CGPointMake(320, 480)];
    [line1 stroke];
    CGContextRestoreGState(context);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

预谋 2024-10-03 07:39:41

就像我上面说的,你的代码对我来说工作得很好。

您是否将 Interface Builder 中视图的“类标识”更改为 UIView 子类?

(顺便说一句,在 viewDidLoad 中调用 setNeedsDisplay 是不必要的,但也不会造成任何损害。)

Like I said above, your code is working fine for me.

Are you changing the "Class Identity" of the view in Interface Builder to your UIView subclass?

(As an aside, calling setNeedsDisplay in viewDidLoad is unnecessary, but it's also not hurting anything.)

情绪失控 2024-10-03 07:39:41

您的图形上下文无效。发生这种情况的原因是:

  • 您自己调用了drawRect:。永远不要那样做。改为调用 setNeedsDisplay 并让 iOS 调用它。
  • 您以某种方式破坏了当前的图形上下文(不太可能)。

Your graphics context is invalid. This happens because:

  • You called drawRect: yourself. Never do that. Call setNeedsDisplay instead and have iOS call it.
  • You somehow destroyed the current graphics context (less likely).
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文