简体   繁体   中英

Draw line between two different point

I want to draw line between two points. Here is my code. but context memory is 0.

CGContextRef context = UIGraphicsGetCurrentContext();

        CGContextSetLineWidth(context, 2.0);

        CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();

        CGFloat components[] = {0.0, 0.0, 1.0, 1.0};

        CGColorRef color = CGColorCreate(colorspace, components);

        CGContextSetStrokeColorWithColor(context, color);

        CGContextMoveToPoint(context, 0, 0);
        CGContextAddLineToPoint(context, 300, 400);

        CGContextStrokePath(context);
        CGColorSpaceRelease(colorspace);
        CGColorRelease(color);

You need to call your code within a UIView subclass in the method drawRect: . UIGraphicsGetCurrentContext() will get NULL (0) when your are not in a drawing context.

You might also check the docs .

Here is a very useful tutorial for your need.

http://trailsinthesand.com/exploring-iphone-graphics-part-1/

Go through it once..!

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