UIGraphicsGetImageFromCurrentImageContext crop to content and not to bounding box

半腔热情 提交于 2019-12-12 00:02:26

问题


I have an area where a user can add their signature to an app, to verify an order.
They sign with their touch.
The only thing is, the frame for the signature is large, and if the user were to make a signature very small, when I save the image, I'm left with a wealth of empty space around the image, which, when I add it to an email further down the process can look horrible.

Is there any way, using that I can crop the image to whatever the bounds of the actual content is, and not the bounds of the box itself?

I would imagine the process would involve somehow detecting the content within the space and drawing a CGRect to match it's bounds, before passing this CGRect back to the context? But I'm not sure how to go about doing this in any way shape or form, this really is my first time using CGContext and the Graphics Frameworks.

Here's my signature drawing 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 it.


回答1:


You can do this with by tracking the min and max touch values.

For example, make some min/max ivars

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

Heres a demo rect

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

Set them up with impossible values. You'll want to do this for each signature not each stroke, but how you do this is up to you. I suggest a reset on 'clear', assuming 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!



来源:https://stackoverflow.com/questions/10313802/uigraphicsgetimagefromcurrentimagecontext-crop-to-content-and-not-to-bounding-bo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!