0

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);
4

2 に答える 2

2

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.

于 2012-04-12T10:47:31.187 に答える
0

Here is a very useful tutorial for your need.

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

Go through it once..!

于 2012-04-12T10:59:32.863 に答える