2012-04-25 4 views
0

私はユーザーが注文を確認するために自分の署名をアプリに追加できる場所があります。
彼らは接触して署名します。
唯一のことは、署名のフレームが大きく、ユーザーが署名を非常に小さくすると、画像を保存するときに画像の周りに豊富な空きスペースが残っていることです。私はプロセスをさらに電子メールに追加すると恐ろしいように見えます。UIGraphicsGetImageFromCurrentImageContextはコンテンツへのクロップと境界ボックスではない

イメージを実際のコンテンツの境界線に合わせて切り抜くことができますが、ボックス自体の境界線ではありません。

私はこのプロセスが何らかの形でスペース内のコンテンツを検出し、このCGRectをコンテキストに戻す前に、その境界に一致するようにCGRectを描画することになると思いますか?しかし、どのような形や形でこれをやっていくのか、私はCGContextとGraphics Frameworkを使って初めてのことです。あなたがそれを提供することができればあなたの助けを

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *touch = [touches anyObject]; 
    CGPoint currentPoint = [touch locationInView:signView]; 

    //Define Properties 
    [drawView.image drawInRect:CGRectMake(0, 0, drawView.frame.size.width, drawView.frame.size.height)]; 
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); 
    CGContextSetLineJoin(UIGraphicsGetCurrentContext(), kCGLineJoinBevel); 
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0); 
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.0, 0.0, 1.0); 
    CGContextSetShouldAntialias(UIGraphicsGetCurrentContext(), true); 
    CGContextSetAllowsAntialiasing(UIGraphicsGetCurrentContext(), true); 

    //Start Path 
    CGContextBeginPath(UIGraphicsGetCurrentContext()); 
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); 
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y); 
    CGContextStrokePath(UIGraphicsGetCurrentContext()); 

    //Save Path to Image 
    drawView.image = UIGraphicsGetImageFromCurrentImageContext(); 

    lastPoint = currentPoint; 

} 

ありがとう:

は、ここに私の署名の描画コードです。

答えて

0

最小と最大のタッチ値をトラッキングすることでこれを行うことができます。

は例えば、いくつかの最小/最大のアイバーズ

@interface ViewController() { 
     CGFloat touchRectMinX_; 
     CGFloat touchRectMinY_; 
     CGFloat touchRectMaxX_; 
     CGFloat touchRectMaxY_; 
     UIView *demoView_; 
    } 
    @end 

- (void)viewDidLoad { 
     [super viewDidLoad]; 
     demoView_ = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)]; 
     demoView_.backgroundColor = [UIColor redColor]; 
     [self.view addSubview:demoView_]; 
    } 

が不可能な値でそれらを設定相続人デモRECTを作ります。それぞれのストロークではなく、各署名ごとにこれを行いたいと思いますが、どのようにするかはあなた次第です。明確なボタンがあることを前提として、「クリア」をリセットすることをお勧めします。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
     touchRectMinX_ = CGFLOAT_MAX; 
     touchRectMinY_ = CGFLOAT_MAX; 
     touchRectMaxX_ = CGFLOAT_MIN; 
     touchRectMaxY_ = CGFLOAT_MIN; 
    } 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
     UITouch *touch = [touches anyObject]; 
     CGPoint currentPoint = [touch locationInView:self.view]; 

     touchRectMinX_ = MIN(touchRectMinX_, currentPoint.x); 
     touchRectMinY_ = MIN(touchRectMinY_, currentPoint.y); 
     touchRectMaxX_ = MAX(touchRectMaxX_, currentPoint.x); 
     touchRectMaxY_ = MAX(touchRectMaxY_, currentPoint.y); 

     // You can use them like this: 
     CGRect rect = CGRectMake(touchRectMinX_, touchRectMinY_, fabs(touchRectMinX_ - touchRectMaxX_), fabs(touchRectMinY_ - touchRectMaxY_)); 
     demoView_.frame = rect; 
     NSLog(@"Signature rect is: %@", NSStringFromCGRect(rect)); 
    } 

は、この情報がお役に立てば幸い変更を記録!

関連する問題