2009-06-24 5 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]; 
} 

、その後

にCurrentPointと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 –

関連する問題