简体   繁体   English

在Objective-C的2点之间画线

[英]draw line between 2 points in objective-c

I want to draw line between 2 points. 我想在2点之间画线。 But UIGraphicsGetCurrentContext() is return null 但是UIGraphicsGetCurrentContext()返回null

ViewController.m ViewController.m

self.drawLine.firstPoint = self.btnPointOne.bounds.origin;
self.drawLine.secondPoint = self.btnPointTwo.bounds.origin;
[self.drawLine drawRect:self.drawWatchModeView.bounds];

DrawLine.h DrawLine.h

@interface DrawLine : UIView{
    CGContextRef context;
}

@property (assign, nonatomic) CGPoint firstPoint;
@property (assign, nonatomic) CGPoint secondPoint;

DrawLine.m DrawLine.m

- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
    }
    return self;
}

- (void)drawRect:(CGRect)rect {
    context = UIGraphicsGetCurrentContext();
    // Drawing code
    CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
    //line width
    CGContextSetLineWidth(context, 1.0);
    CGContextMoveToPoint(context, self.firstPoint.x, self.firstPoint.y);
    CGContextAddLineToPoint(context, self.secondPoint.x, self.secondPoint.y);
    // and now draw the Path!
    CGContextStrokePath(context);
}

But it error 但是它出错

CGContextSetStrokeColorWithColor: invalid context 0x0. CGContextSetStrokeColorWithColor:无效的上下文0x0。 If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable. 如果要查看回溯,请设置CG_CONTEXT_SHOW_BACKTRACE环境变量。
CGContextSetLineWidth: invalid context 0x0. CGContextSetLineWidth:无效的上下文0x0。 If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable. 如果要查看回溯,请设置CG_CONTEXT_SHOW_BACKTRACE环境变量。
CGContextMoveToPoint: invalid context 0x0. CGContextMoveToPoint:无效的上下文0x0。 If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable. 如果要查看回溯,请设置CG_CONTEXT_SHOW_BACKTRACE环境变量。
CGContextAddLineToPoint: invalid context 0x0. CGContextAddLineToPoint:无效的上下文0x0。 If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable. 如果要查看回溯,请设置CG_CONTEXT_SHOW_BACKTRACE环境变量。
CGContextDrawPath: invalid context 0x0. CGContextDrawPath:无效的上下文0x0。 If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable. 如果要查看回溯,请设置CG_CONTEXT_SHOW_BACKTRACE环境变量。
CGContextSetStrokeColorWithColor: invalid context 0x0. CGContextSetStrokeColorWithColor:无效的上下文0x0。 If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable. 如果要查看回溯,请设置CG_CONTEXT_SHOW_BACKTRACE环境变量。
CGContextSetLineWidth: invalid context 0x0. CGContextSetLineWidth:无效的上下文0x0。 If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable. 如果要查看回溯,请设置CG_CONTEXT_SHOW_BACKTRACE环境变量。
CGContextMoveToPoint: invalid context 0x0. CGContextMoveToPoint:无效的上下文0x0。 If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable. 如果要查看回溯,请设置CG_CONTEXT_SHOW_BACKTRACE环境变量。
CGContextAddLineToPoint: invalid context 0x0. CGContextAddLineToPoint:无效的上下文0x0。 If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable. 如果要查看回溯,请设置CG_CONTEXT_SHOW_BACKTRACE环境变量。
CGContextDrawPath: invalid context 0x0. CGContextDrawPath:无效的上下文0x0。 If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable. 如果要查看回溯,请设置CG_CONTEXT_SHOW_BACKTRACE环境变量。
CGContextSetStrokeColorWithColor: invalid context 0x0. CGContextSetStrokeColorWithColor:无效的上下文0x0。 If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable. 如果要查看回溯,请设置CG_CONTEXT_SHOW_BACKTRACE环境变量。
CGContextSetLineWidth: invalid context 0x0. CGContextSetLineWidth:无效的上下文0x0。 If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable. 如果要查看回溯,请设置CG_CONTEXT_SHOW_BACKTRACE环境变量。
CGContextMoveToPoint: invalid context 0x0. CGContextMoveToPoint:无效的上下文0x0。 If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable. 如果要查看回溯,请设置CG_CONTEXT_SHOW_BACKTRACE环境变量。
CGContextAddLineToPoint: invalid context 0x0. CGContextAddLineToPoint:无效的上下文0x0。 If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable. 如果要查看回溯,请设置CG_CONTEXT_SHOW_BACKTRACE环境变量。
CGContextDrawPath: invalid context 0x0. CGContextDrawPath:无效的上下文0x0。 If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable. 如果要查看回溯,请设置CG_CONTEXT_SHOW_BACKTRACE环境变量。
CGContextSetStrokeColorWithColor: invalid context 0x0. CGContextSetStrokeColorWithColor:无效的上下文0x0。 If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable. 如果要查看回溯,请设置CG_CONTEXT_SHOW_BACKTRACE环境变量。
CGContextSetLineWidth: invalid context 0x0. CGContextSetLineWidth:无效的上下文0x0。 If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable. 如果要查看回溯,请设置CG_CONTEXT_SHOW_BACKTRACE环境变量。
CGContextMoveToPoint: invalid context 0x0. CGContextMoveToPoint:无效的上下文0x0。 If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable. 如果要查看回溯,请设置CG_CONTEXT_SHOW_BACKTRACE环境变量。
CGContextAddLineToPoint: invalid context 0x0. CGContextAddLineToPoint:无效的上下文0x0。 If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable. 如果要查看回溯,请设置CG_CONTEXT_SHOW_BACKTRACE环境变量。
CGContextDrawPath: invalid context 0x0. CGContextDrawPath:无效的上下文0x0。 If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable. 如果要查看回溯,请设置CG_CONTEXT_SHOW_BACKTRACE环境变量。

I try to UIViewControllerBasedStatusBarAppearance set YES it not work for me I try to remove UIViewControllerBasedStatusBarAppearance it not work for me 我尝试UIViewControllerBasedStatusBarAppearanceYES它不适合我工作,我尝试删除UIViewControllerBasedStatusBarAppearance它不适合我

You can't just call drawRect: whenever you want, so your line: 您不能只在需要时调用drawRect: ,所以您的代码行:

[self.drawLine drawRect:self.drawWatchModeView.bounds];

needs to change to ask the OS to run a draw cycle. 需要更改以要求操作系统运行绘制周期。 When that happens the drawing context will be setup and everything will be ready. 发生这种情况时,将设置绘图上下文,并且一切就绪。 When you run it explicitly that context doesn't exist. 当您明确运行它时,该上下文不存在。 So, change that line to: 因此,将该行更改为:

[self.drawLine setNeedsDisplay];

or 要么

[self.drawLine setNeedsDisplayInRect:self.drawWatchModeView.bounds];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM