0

注文を確認するために、ユーザーが署名をアプリに追加できる領域があります。
彼らはタッチで署名します。
唯一のことは、署名のフレームが大きく、ユーザーが署名を非常に小さくした場合、画像を保存すると、画像の周りに豊富な空白が残ります。これを追加すると、プロセスのさらに下にある電子メールにすると、恐ろしいように見える可能性があります。

ボックス自体の境界ではなく、実際のコンテンツの境界が何であれ、画像をトリミングできる方法はありますか?

このプロセスには、この CGRect をコンテキストに戻す前に、スペース内のコンテンツを何らかの方法で検出し、その境界に一致するように CGRect を描画することが含まれると思いますか? しかし、どのような形や形でこれを行うべきかわかりません。CGContext とグラフィックス フレームワークを使用するのはこれが初めてです。

これが私の署名の描画コードです。

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

}

もしよろしければご提供いただけると助かります。

4

1 に答える 1

0

これは、タッチの最小値と最大値を追跡することで実行できます。

たとえば、いくつかの最小/最大 ivar を作成します。

    @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_];  
    }

無理な値で設定してください。ストロークごとではなく、署名ごとにこれを行う必要がありますが、これを行う方法はあなた次第です。クリアボタンがあると仮定して、「クリア」でリセットすることをお勧めします。

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

お役に立てれば!

于 2012-04-25T13:30:04.003 に答える