从裁剪后的图片中去除不需要的区域

3

我正在使用 UIBezierPath 来通过触摸裁剪图像。

我可以正确地裁剪出所选区域,但不需要的区域(不在裁剪部分内)仍然占用空间。或者换句话说,我无法摆脱裁剪区域之外的区域。

那么,如何去除这个不必要/无用的区域呢?

下面是我的代码:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UIView *touchedView = [delegate viewForUseWithTool:self];
    for (UITouch *touch in [event allTouches]) {
        // remember the touch, and its original start point, for future
        [trackingTouches addObject:touch];
        CGPoint location = [touch locationInView:touchedView];
        [startPoints addObject:[NSValue valueWithCGPoint:location]];
        UIBezierPath *path = [UIBezierPath bezierPath];
        path.lineCapStyle = kCGLineCapRound;
        [path moveToPoint:location];
        [path setLineWidth:delegate.strokeWidth];
        [path addLineToPoint:location];
        [paths addObject:path];
    }
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    for (UITouch *touch in [event allTouches]) {
        // make a line from the start point to the current point
        NSUInteger touchIndex = [trackingTouches indexOfObject:touch];
        // only if we actually remember the start of this touch...
        if (touchIndex != NSNotFound) {
            UIBezierPath *path = [paths objectAtIndex:touchIndex];
            PathDrawingInfo *info = [PathDrawingInfo pathDrawingInfoWithPath:path fillColor:[UIColor clearColor] strokeColor:delegate.strokeColor];
            [delegate addDrawable:info];
            [trackingTouches removeObjectAtIndex:touchIndex];
            [startPoints removeObjectAtIndex:touchIndex];
            [paths removeObjectAtIndex:touchIndex];
        }
    }
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    for (UITouch *touch in [event allTouches]) {
        // make a line from the start point to the current point
        NSUInteger touchIndex = [trackingTouches indexOfObject:touch];
        // only if we actually remember the start of this touch...
        if (touchIndex != NSNotFound) {
            UIBezierPath *path = [paths objectAtIndex:touchIndex];
            PathDrawingInfo *info = [PathDrawingInfo pathDrawingInfoWithPath:path fillColor:[UIColor clearColor] strokeColor:delegate.strokeColor];
            [delegate addDrawable:info];
            [trackingTouches removeObjectAtIndex:touchIndex];
            [startPoints removeObjectAtIndex:touchIndex];
            [paths removeObjectAtIndex:touchIndex];
        }
    }
}
1个回答

0

我能够通过遵循这个答案来调整我的结果图像的尺寸。 对于Retina显示屏,我需要将cropRect的大小加倍,并借助这个答案来检测Retina显示屏。

编辑

我找到了一个更好的解决方案。以下是步骤:

  1. UIImage转换为UIImageView
  2. 遵循这个答案

我找到了一个更好的解决方案。首先将 UIImage 转换为 UIImageView,然后按照 [这个答案][1] 进行操作。 [1]: https://dev59.com/8FXTa4cB1Zd3GeqPzz8u#7210247 - Sufian

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接