1

In my application I've used a UIImagePickerController to take a photo, and after I took the photo, I can't draw it using UIImageView because I want to draw some lines on it with my finger.

To this end I have written in draw rect methods this code:

- (void)drawRect:(CGRect)rect { 
    [self.myimage drawInRect: rect];

    //Disegno rettangolo del tag
    CGContextRef myContext = UIGraphicsGetCurrentContext();

    CGContextSetRGBStrokeColor(myContext, 0.5, 0.5, 0.5, 1.0);

    CGContextSetLineWidth(myContext, 3.0f);

    CGContextAddPath(myContext, pathTouches);

    CGContextStrokePath(myContext);

}

and in touchesMoved:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [touches anyObject];
    CGPoint locationInView = [touch locationInView:self];

    // Draw a connected sequence of line segments
    CGPoint addLines[] =
    {
        //....
            //some lines
    };

    CGMutablePathRef newPath = CGPathCreateMutable();
    CGPathRelease(pathTouches);
    pathTouches = CGPathCreateMutableCopy(newPath);
    CGPathRelease(newPath);

    CGPathAddLines(pathTouches, NULL, addLines, sizeof(addLines)/sizeof(addLines[0]));

    [self setNeedsDisplay];

}

Is it correct to call [self setNeedsDisplay]; every time I move my finger, or there is a better way to do this?

Jason Plank
  • 2,336
  • 5
  • 31
  • 40
Alex
  • 91
  • 3
  • 6

1 Answers1

1

i have done this but stuck at another problem...here is your solution...

- (void)imagePickerController:(UIImagePickerController *)picker 
    didFinishPickingImage:(UIImage *)image
              editingInfo:(NSDictionary *)editingInfo
{
// Dismiss the image selection, hide the picker and
//show the image view with the picked image
[picker dismissModalViewControllerAnimated:YES];
imagePickerController.view.hidden = YES;
[imagePickerController release];
imageView.image = image;
imageView.hidden = NO;
[imageView setFrame:CGRectMake(0, 20, 320, 396)];
[window bringSubviewToFront:imageView];
}

then

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if ([touch view] == EditImageView)
{
    lastPoint = currentPoint;
    lastPoint = [touch locationInView:self.view];

UIGraphicsBeginImageContext(EditImageView.frame.size);
[EditImageView.image drawInRect:CGRectMake(0, 0, EditImageView.frame.size.width, EditImageView.frame.size.height)];

//sets the style for the endpoints of lines drawn in a graphics context
ctx = UIGraphicsGetCurrentContext();
CGContextSetLineCap(ctx, kCGLineCapRound);
//sets the line width for a graphic context
CGContextSetLineWidth(ctx,10.0);
//set the line colour
CGContextSetRGBStrokeColor(ctx, 1.0, 0.0, 0.0, 1.0);
//creates a new empty path in a graphics context
CGContextBeginPath(ctx);
    CGContextSetAllowsAntialiasing(ctx,TRUE);
//begin a new path at the point you specify
CGContextMoveToPoint(ctx, currentPoint.x, currentPoint.y);
//Appends a straight line segment from the current point to the provided point 
CGContextAddLineToPoint(ctx, lastPoint.x,lastPoint.y);
//paints a line along the current path
CGContextStrokePath(ctx);
EditImageView.image = UIGraphicsGetImageFromCurrentImageContext();
    [EditImageView setNeedsDisplay];

    CGContextSaveGState(ctx);
    UIGraphicsEndImageContext();
     currentPoint = lastPoint;
}   
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject]; 
if ([touch view] == EditImageView)
{
    currentPoint=[touch locationInView:self.view];
}
}

where currentPoint and lastPoint are CGPoint Declared in Header File.

Rahul Vyas
  • 28,260
  • 49
  • 182
  • 256
  • hi, thanks for your code, I tried to draw on a simple uiview over my app, but doesn't work... can you help me?? thanks – ghiboz Jun 10 '10 at 23:15
  • @Rahul Vyas Can you help me to solve this issue : http://stackoverflow.com/q/9850772/434898 – Ajay Sharma Mar 26 '12 at 11:18