2012-04-25 44 views
0

我有一個區域,用戶可以將其簽名添加到應用程序以驗證訂單。
他們用他們的觸摸標誌。
唯一的問題是,簽名的框架很大,如果用戶要使簽名非常小,當我保存圖像時,我會在圖像周圍留下大量空白空間,當我把它添加到進一步的電子郵件過程看起來很可怕。UIGraphicsGetImageFromCurrentImageContext裁剪到內容而不是包圍盒

有沒有什麼辦法,使用我可以將圖像裁剪到任何實際內容的邊界,而不是框的邊界本身?

我會想象這個過程會涉及到某種方式檢測空間內的內容並繪製一個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; 

} 

感謝您的幫助,如果你可以提供它。

回答

0

您可以通過跟蹤最小和最大觸摸值來完成此操作。

例如,使一些最大/最小的ivars

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

希望這有助於!