绘制矩形上下文错误
我听说很多人不使用drawRect 都会遇到上下文错误
现在我有这个:
- (void)drawRect:(CGRect)rect {
NSLog(@"drawRect: Starts");
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0);
CGContextSetLineWidth(context, 3.0);
CGContextMoveToPoint(context, lineStart.x, lineStart.y);
CGContextAddLineToPoint(context, lineEnd.x, lineEnd.y);
CGContextStrokePath(context);
}
错误:
<Error>: CGContextSetRGBStrokeColor: invalid context
它在以前的程序上有效,但在这个程序上无效。有什么不同:我有一个视图控制器,它调用这个 UIView:
-(void)createLine:(CGPoint)start:(CGPoint)end {
NSLog(@"createLine: Starts");
lineEnd = start;
lineStart = end;
self = [super initWithFrame:drawRect:CGRectMake(fmin(lineStart.x, lineEnd.x), fmin(lineStart.y, lineEnd.y), fabs(lineStart.x - lineEnd.x), fabs(lineStart.y - lineEnd.y))];
}
这是我的第一个问题,我不确定我应该在这里放多少信息代码,所以对我来说容易一些。
I heard that a lot of people get a context error by not using drawRect
Now I have this:
- (void)drawRect:(CGRect)rect {
NSLog(@"drawRect: Starts");
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0);
CGContextSetLineWidth(context, 3.0);
CGContextMoveToPoint(context, lineStart.x, lineStart.y);
CGContextAddLineToPoint(context, lineEnd.x, lineEnd.y);
CGContextStrokePath(context);
}
Error:
<Error>: CGContextSetRGBStrokeColor: invalid context
Which had work on previous programs, but not on this one. Whats different: I have a view controller, which calls this UIView:
-(void)createLine:(CGPoint)start:(CGPoint)end {
NSLog(@"createLine: Starts");
lineEnd = start;
lineStart = end;
self = [super initWithFrame:drawRect:CGRectMake(fmin(lineStart.x, lineEnd.x), fmin(lineStart.y, lineEnd.y), fabs(lineStart.x - lineEnd.x), fabs(lineStart.y - lineEnd.y))];
}
This is my first question, and I am not sure how much info code I should be putting here so be easy on me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
冒着陈述显而易见的风险,看起来您在没有有效上下文的情况下有时会调用
createLine
。这就是为什么直接调用drawRect
不是一个好主意。那么,调用setNeedsDisplay
并让系统在合适的时间调用drawRect
。At the risk of stating the obvious, it looks like you're calling
createLine
sometime when you don't have a valid context. This is why it's not a good idea to calldrawRect
directly. Intsead, callsetNeedsDisplay
and let the system calldrawRect
at a suitable time.正如 walkytalky 所写,您不应该直接调用
drawRect
。如果您需要确保触发对drawRect
的调用,请调用setNeedsDisplay
,这将确保在适当的时候调用drawRect
时间。createLine::
方法的最后一行有点令人困惑且不正统。我认为出于您的目的,您需要删除该行并将其替换为 all tosetNeedsDisplay
。这应该会使用新的 lineStart 和 lineEnd 值触发对drawRect
的调用。As walkytalky wrote, you should not call
drawRect
directly. If you need to make sure that a call todrawRect
is triggered, then callsetNeedsDisplay
, which will make sure thatdrawRect
is called at the appropriate time.The last line of your
createLine::
method is a bit confusing and unorthodox. I think that for your purposes that you want to delete that line and simply replace it with a all tosetNeedsDisplay
. That should trigger a call todrawRect
with the new lineStart and lineEnd values.