从裁剪的图像中删除不需要的区域

时间:2012-06-28 09:52:17

标签: iphone crop uibezierpath

我正致力于通过触摸裁剪图像。为此,我使用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 :(得分:0)

我可以按照this answer调整生成的图片大小。 对于视网膜显示,我不得不将cropRect的大小加倍,并在this answer的帮助下检测到视网膜显示。

修改

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

  1. UIImage转换为UIImageView
  2. 关注this answer