简体   繁体   中英

CGPath not visible

I want to create a CGPath to look like a trail in an overview perspective. I have the following code in a UIView subclass. I know I am entering the methods through NSLogs. The CGPath doesn't appear on the screen.

- (void)drawRect:(CGRect)rect{
  CGMutablePathRef p = CGPathCreateMutable();
  CGPathMoveToPoint( p, NULL , 150 , 150 );
  CGPathMoveToPoint( p, NULL , 300 , 300 );
  CGPathMoveToPoint( p, NULL , 100 , 150 ) ;
  CGPathMoveToPoint( p, NULL , 150 , 150 );
  CGPathAddLineToPoint( p, NULL , 300 , 300 );
  CGPathAddLineToPoint( p, NULL , 100 , 150 ) ;
  CGPathAddLineToPoint( p, NULL , 150 , 150 );

  CGPathCloseSubpath( p ) ;

  CGContextRef context=UIGraphicsGetCurrentContext(); 
  CGContextBeginPath(context);
  CGContextAddPath ( context, p ) ;
  CGContextSaveGState(context); 

  CGColorRef redColor = [UIColor colorWithRed:1.0 green:12.0 blue:0.0 alpha:0.0].CGColor;
  CGContextSetFillColorWithColor(context, redColor);
  CGContextSetLineWidth(context, 20.0);
  CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);

  CGContextStrokePath(context);
  CGContextFillPath ( context ) ;

  CGContextDrawPath(context, kCGPathFill);
  CGContextRestoreGState(context);
}

Nothing shows up In advance thanks

The frameworks ensure that in -drawRect: there is already an appropriate graphics context set up for you and pointed at the display device. You only need UIGraphicsBeginImageContext() / UIGraphicsEndImageContext() if you are setting up to render someplace else, such as to an image.

Delete those calls and things will behave a lot better. Right now you are rendering to an off-screen buffer, not to the view itself.

Edit: You should, however, add a pair of CGContextSaveGState() / CGContextRestoreGState() calls around your code, however. This will ensure you don't contaminate the graphics context for other graphics code if your -drawRect: is used in certain ways.

Edit 2: Alright, I see that you have resolved the issue after your last response, but for the benefit of archives, I am going to point out that you never call -drawRect: directly (except in some obscure cases that don't apply here). Since you had called it directly, no graphics context had been set up, hence the crash. The proper way to trigger redraw is to send the view -setNeedsDisplay ; this will cause the frameworks to setup a graphics context and invoke -drawRect: for you at the appropriate time in the run loop.

If on iOS, switch to bezier paths using UIBezierPath. Sure-fire to work. Also, I've noticed, each of your moveToPoints are in a single row, when they should have one addLineToPoint right after them, like so:

CGPathMoveToPoint(p, NULL, 150, 150); CGPathAddLineToPoint( p, NULL, 300, 300);

CGPathMoveToPoint(p, NULL, 300, 300 ; CGPathAddLineToPoint(p, NULL, 100, 150);

CGPathMoveToPoint(p, NULL, 100, 150); CGPathAddLineToPoint(p, NULL, 100, 150);

CGPathMoveToPoint(p, NULL, 150, 150); CGPathAddLineToPoint(p, NULL, 150, 150);

But you'll want to switch to UIBezierPaths anyway once you see how easy they are.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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