2009-06-24 3 views
1

내 응용 프로그램에서는 UIImagePickerController을 사용하여 사진을 찍었습니다. 사진을 찍은 후에는 손가락으로 일부 선을 그려야하기 때문에 UIImageView을 사용하여 그릴 수 없습니다.UIImage를 CurrentContext에 그리기

- (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 : 나는 무승부 RECT 방법이 코드를 작성한이를 위해

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

다음 현재 점과 lastPoint가 CGPoint는 헤더 파일에 선언되어있다

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

.

+0

안녕하세요, 코드 덕분에, 내 애플 리케이션을 통해 간단한 uiview에 그려려고했지만 작동하지 않습니다 ... 당신이 나를 도울 수 있습니까 ?? 감사합니다. – ghiboz

+0

@ Rahul Vyas이 문제를 해결할 수 있도록 도와 줄 수 있습니까? http://stackoverflow.com/q/9850772/434898 –

관련 문제