Gurumoorthy Arumugam
Gurumoorthy Arumugam

Reputation: 2128

drawing Line using NSpoint pixels with array

i have store my NSPoint in array like this. i want to give that array input to another class to drawing same operation. how to draw that particular array value.

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];   
    CGPoint currentPoint = [touch locationInView:self];

    CGPathAddLineToPoint(self.currentLine.linePath, NULL, currentPoint.x, currentPoint.y);
    [self setNeedsDisplay];

    [_array addObject:[NSValue valueWithCGPoint: currentPoint]];

}

here is my array value console output

"NSPoint: {745, 575}",
"NSPoint: {730, 584}",
"NSPoint: {717, 588}",
"NSPoint: {701, 590}",
"NSPoint: {678, 590}",
"NSPoint: {642, 590}",
"NSPoint: {590, 590}",
"NSPoint: {520, 590}",
"NSPoint: {465, 589}",
"NSPoint: {438, 587}",
"NSPoint: {415, 587}",
"NSPoint: {403, 582}"


       - (void)drawRect:(CGRect)rect
        {
//    CGContextSetAlpha(context, self.currentLine.opacity);    
//        CGContextSetStrokeColorWithColor(context, self.currentLine.lineColor.CGColor);
//        CGContextSetLineWidth(context, self.currentLine.lineWidth);
//        CGContextSetLineCap(context, kCGLineCapRound);
//        CGContextSetLineJoin(context, kCGLineJoinRound);
 //       CGContextBeginPath(context);
//        CGContextAddPath(context, self.currentLine.linePath);
 //       CGContextStrokePath(context);
???????????
        }

this code will using for touch input line drawing.how to draw line with out user interaction when view will apper .this subclass uiview. i can able to draw using mouse.my need is i have CGPoint value Array i want to pass that Array to this class input for drawing line.how can i pass that

Upvotes: 0

Views: 965

Answers (3)

Abizern
Abizern

Reputation: 150615

You need to get the points values out of the array and create a path out of them, and then stroke the path.

- (void)drawRect:(CGRect)rect {
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    // Set up your conext here, line colour, thickness, endcaps etc
    CGContextSetStrokeColorWithColor(ctx, [UIColor redColor].CGColor);
    CGContextSetLineWidth(ctx, 2.0);
    CGContextSetFillColorWithColor(ctx, [UIColor redColor].CGColor);

    //Create a path
    CGMutablePathRef pathRef = CGPathCreateMutable();

    // assume your array is called pointsArray and is a property
    // Move to the starting point
    CGPoint pt = [[self.pointsArray objectAtIndex:0] CGPointValue];
    CGPathMoveToPoint(pathRef, NULL, pt.x, pt.y);

    // Add the other points to the path
    CFIndex arrayCount = [self.pointsArray count];

    for (CFIndex i = 1; i < arrayCount; i++) {
        pt = [[self.pointsArray objectAtIndex:i] CGPointValue];
        CGPathAddLineToPoint(pathRef, NULL, pt.x, pt.y);
    }

    // Now you have a path, stroke it
    CGContextAddPath(ctx, pathRef);
    CGContextStrokePath(ctx);

    // Release your pathRef
    CGPathRelease(pathRef);
}

You can download a small Example Xcode project that shows this working.

Upvotes: 1

Hector
Hector

Reputation: 3907

  CGContextMoveToPoint(context, 0.0, 0.0);
  for (NSValue* value in array)
    {
      CGPoint p =[value CGPointValue];
      CGContextAddLineToPoint(context,p.x,p.y);

    }

put it in -(void)drawInContext:(CGContextRef)context method

Try This may be helpful for you...

Upvotes: 0

Inder Kumar Rathore
Inder Kumar Rathore

Reputation: 39988

Do this in your current class where you want to pass array

A.h

{
    NSArray *mArray;
}
@property (nonatomic, retain) NSArray *mArray;

A.m

@synthesize mArray;

then write this where you have created A's object

A *a = [[A alloc] init]];
//this code to set the array in your A class
a.mArray = yourArrayOfPoints;

Upvotes: 0

Related Questions