UIGraphicsGetImageFromCurrentImageContext裁剪到内容而不是边框 [英] UIGraphicsGetImageFromCurrentImageContext crop to content and not to bounding box

查看:92
本文介绍了UIGraphicsGetImageFromCurrentImageContext裁剪到内容而不是边框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个区域,用户可以在应用程序中添加其签名以验证订单。

他们用触摸进行签名。

唯一的是框架因为签名很大,并且如果用户要使签名非常小,那么当我保存图像时,图像周围会留有大量的空白空间,当我将其添加到电子邮件中的更下方时,过程看起来很可怕。

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?

我想该过程将以某种方式检测空间中的内容并绘制一个CGRect以匹配其边界,然后再将该CGRect传递回上下文?但是我不确定如何以任何形式进行此操作,这确实是我第一次使用CGContext和图形框架。

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.

推荐答案

您可以通过跟踪最小和最大接触值来做到这一点。

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

在此处演示演示

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

现在记录更改

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

希望这会有所帮助!

这篇关于UIGraphicsGetImageFromCurrentImageContext裁剪到内容而不是边框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆