2016-05-02 12 views
1

画像に描画するコードは次のとおりです。panGestureを使用して、ユーザーが触れる場所を探します。今私がこのコードを使用するとき、私が手を画像上で非常に速く動かしているときに、ユーザが描く線がポイントとなる。PAN GESTUREを使用して別の画像に連続線を描画することはできません

UIGraphicsBeginImageContextWithOptions((self.thatsMyImage.frame.size), NO, 0.0); 
    [self.selfieImage.image drawInRect:CGRectMake(0,0,self.thatsMyImage.frame.size.width, self.thatsMyImage.frame.size.height)]; 
[[UIColor whiteColor] set]; 
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), from.x, from.y); 
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), to.x , to.y); 
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 10.0f); 
CGContextStrokeEllipseInRect(UIGraphicsGetCurrentContext(), CGRectMake(to.x, to.y,10,10)); 

CGContextSetFillColor(UIGraphicsGetCurrentContext(), CGColorGetComponents([[UIColor blueColor] CGColor])); 
CGContextFillPath(UIGraphicsGetCurrentContext()); 

CGContextStrokePath(UIGraphicsGetCurrentContext()); 

self.thatsMyImage.image = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

これはpanGestureが検出されたときに呼び出されるメソッドです。

-(void)freeFormDrawing:(UIPanGestureRecognizer *)gesture 
{ 
if(gesture.state == UIGestureRecognizerStateChanged) 
{ 
    CGPoint p = [gesture locationInView:self.selfieImage]; 
    CGPoint startPoint = lastPoint; 
    lastPoint = [gesture locationInView:self.selfieImage]; 

    [self drawLineFrom:startPoint endPoint:p]; 

} 
if(gesture.state == UIGestureRecognizerStateEnded) 
{ 
//  lastPoint = [gesture locationInView:self.selfieImage]; 
    } 
} 

滑らかな線と曲線を持つ画像でフリーフォーム(Doodle)をどうやって行うことができますか?事前に感謝し、ハッピーコーディング!

答えて

1

私はUIImageViewの上に透明なUIImageViewを追加していたのと同じことを実装しました。UIImageViewの上に私が描きたかったUIImageを持っていました。

UIImageView *drawableView = [[UIImageView alloc]initWithFrame:self.drawImageView.bounds]; 
drawableView.userInteractionEnabled = YES; 
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(drawingViewDidPan:)]; 
[drawableView addGestureRecognizer:panGesture]; 

は、その後、ユーザがUIImageViewにパンしたときに、私は

- (void)drawingViewDidPan:(UIPanGestureRecognizer*)sender 
{ 
    CGPoint currentDraggingPosition = [sender locationInView:drawableView]; 

    if(sender.state == UIGestureRecognizerStateBegan) { 
     prevDraggingPosition = currentDraggingPosition; 
    } 

    if(sender.state != UIGestureRecognizerStateEnded){ 
     [self drawLine:prevDraggingPosition to:currentDraggingPosition]; 
    } 
    prevDraggingPosition = currentDraggingPosition; 
} 

この関数を呼び出しますprevDraggingPositionとcurrentDraggingPositionの両方がするCGPointです。

は、その後、私はあなたがあなたのUIImageViewへdrawableViewの画像を描画することでイメージを構築することができ、その上に描画して画像を取得するために、最終的にはその後
-(void)drawLine:(CGPoint)from to:(CGPoint)to 
{ 
    @autoreleasepool { 

    CGSize size = drawableView.frame.size; 
    UIGraphicsBeginImageContextWithOptions(size, NO, 0.0); 

    CGContextRef context = UIGraphicsGetCurrentContext(); 

    [drawableView.image drawAtPoint:CGPointZero]; 

    CGFloat strokeWidth = 4.0; 
    strokeColor = colorChangeView.backgroundColor; 

    CGContextSetLineWidth(context, strokeWidth); 
    CGContextSetStrokeColorWithColor(context, strokeColor.CGColor); 
    CGContextSetLineCap(context, kCGLineCapRound); 


    CGContextMoveToPoint(context, from.x, from.y); 
    CGContextAddLineToPoint(context, to.x, to.y); 
    CGContextStrokePath(context); 

    drawableView.image = UIGraphicsGetImageFromCurrentImageContext(); 

    UIGraphicsEndImageContext(); 
    } 
} 

をcurrentDraggingPositionするprevDraggingPositionから線を描画するには、以下の機能を使用していますこのようなあなたのイメージがあります。 originalImageSizeがあなたのイメージのサイズである

- (UIImage*)buildImage 
{ 
    @autoreleasepool { 

    UIGraphicsBeginImageContextWithOptions(originalImageSize, NO, self.drawImageView.image.scale); 

    [self.drawImageView.image drawAtPoint:CGPointZero]; 
    [drawableView.image drawInRect:CGRectMake(0,0, originalImageSize.width, originalImageSize.height)]; 

    UIImage *tmp = UIGraphicsGetImageFromCurrentImageContext(); 

    UIGraphicsEndImageContext(); 

    return tmp; 
    } 
} 

希望すると便利です。

+0

Avinash Dadhichさん、ありがとうございました。私は私の必要性を満たしています。 – Alex

関連する問題