2009-06-24 3 views
1

В моем приложении я использовал UIImagePickerController, чтобы сделать снимок, и после того, как я сделал снимок, я не могу нарисовать его, используя UIImageView, потому что я хочу нарисовать несколько строк на нем пальцем.Рисование UIImage в CurrentContext

С этой целью я написал в методах Жеребьевка Rect этот код:

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

} 

и в 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]; 

} 

ли правильно называть [self setNeedsDisplay]; каждый раз, когда я пошевелить пальцем, или там это лучший способ сделать это?

ответ

1

я сделал это, но застрял на другую проблему ... вот ваше решение ...

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

затем

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

где currentPoint и lastPoint являются CGPoint объявленная в заголовке файла.

+0

привет, спасибо за ваш код, я попытался нарисовать простой uiview над своим приложением, но не работает ... можете ли вы мне помочь? спасибо – ghiboz

+0

@Rahul Vyas Можете ли вы помочь мне решить эту проблему: http://stackoverflow.com/q/9850772/434898 –

Смежные вопросы