3

I am working on cropping an image via touch. For this, I am using UIBezierPath.

I can crop out the selected region alright, but the unneeded region (not inside the cropped part) is not still taking space. Or in other words, I can not get rid of the area which lies outside my cropped area.

So, how can I remove this unneeded/useless area?

Here is my code:

- (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];
        }
    }
}
Sufian
  • 6,405
  • 16
  • 66
  • 120

1 Answers1

0

I was able to resize my resulting image by following this answer. For retina display, I had to double the size of cropRect and I detected retina display with the help of this answer.

Edit

I found an even better solution. Here are the steps:

  1. convert the UIImage to UIImageView,
  2. follow this answer.
Community
  • 1
  • 1
Sufian
  • 6,405
  • 16
  • 66
  • 120
  • I found out an even better solution. First convert the `UIImage` to `UIImageView` and then follow [this answer][1]. [1]: http://stackoverflow.com/a/7210247/1276636 – Sufian Jul 26 '12 at 07:10