2009-06-24 9 views
1

Dans mon application, j'ai utilisé un UIImagePickerController pour prendre une photo, et après avoir pris la photo, je ne peux pas la dessiner en utilisant UIImageView car je veux tracer quelques lignes dessus avec mon doigt.Dessin UIImage dans CurrentContext

A cette fin, je l'ai écrit dans les méthodes draw rect ce 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); 

} 

et 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]; 

} 

Est-il correct d'appeler [self setNeedsDisplay]; chaque fois que je déplace mon doigt, ou il est une meilleure façon de faire cela?

Répondre

1

je l'ai fait, mais coincé à un autre problème ... voici votre 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]; 
} 

puis

- (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]; 
} 
} 

où currentPoint et lastPoint sont CGPoint Déclaré dans le fichier en-tête.

+0

Salut, merci pour votre code, j'ai essayé de dessiner sur un simple uiview sur mon application, mais ne fonctionne pas ... pouvez-vous m'aider ?? merci – ghiboz

+0

@Rahul Vyas Pouvez-vous m'aider à résoudre ce problème: http://stackoverflow.com/q/9850772/434898 –