UIGraphicsGetImageFromCurrentImageContext trims content, not limits a window

I have an area where the user can add his signature to the application to verify the order.
They sign with their touch.
The only thing is that the frame for the signature is large, and if the user had to make the signature very small, when I save the image, I have left an empty space around the image, which when I add this letter can still look awful down.

Is there any way using this, I can crop the image regardless of the borders of the actual content, and not on the borders of the window itself?

I would suggest that a process must somehow detect the contents in space and draw a CGRect so that it matches the boundaries before passing this CGRect back to the context? But I'm not sure how to do this in form or form, this is really my first time using CGContext and graphics platforms.

Here is my signature code code:

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

Thanks for your help if you can offer this.

0
objective-c cgrect cgcontext
source share
1 answer

You can do this by tracking the minimum and maximum touch values.

For example, do min / max ivars

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

Here is the daemon release

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

Set them with impossible values. You want to do this for every signature, not every hit, but how you do it is up to you. I suggest reset on 'clear' if you have a clear button.

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

Now record the changes

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

Hope this helps!

0
source share

All Articles