0

I'm trying to build a simple photo crop utility where user can touch and select the picture boundary. I need the exact touch coordinates w.r.t the image.

When I use

    CGImageRef imageRef = [staticBG.image CGImage];
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInView:[self view]];
    y3=location.y;
    x3=location.x;

the coordinates (x3,y3) comes in absolute scale and is not useful for handling imageRef.

I tried using

    UITouch *touch = [touches anyObject];
    CGPoint touchPoint = [touch locationInView:staticBG];

but still the coordinates do not quite match up.

Please help.

metsburg
  • 2,021
  • 1
  • 20
  • 32
  • But if you know the position of the image in the view and y3 and x3 is the touch position in the view couldn't you quite simply calculate the relative coordinates? Or have I understood you incorrectly? – Groot Feb 06 '13 at 08:29
  • yes, that I what I tried to do, but the end result doesn't match up. I expected it to... but it let me down :( – metsburg Feb 06 '13 at 09:24

2 Answers2

2

use following code :)

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

        UITouch *touch = [touches anyObject];

        if ([touch tapCount] == 2) {
            //self.imageView.image = nil;
            return;
        }

        CGPoint lastPoint = [touch locationInView:self.YourImageView];
        NSLog(@"%f",lastPoint.x);
        NSLog(@"%f",lastPoint.y);

    }
iPatel
  • 46,010
  • 16
  • 115
  • 137
0

I don't think we need exact coordinates from the user touch, but to crop the image I will :

  1. Layout a maskView on the image to let the user choose the area to crop. The maskView has a exact frame that can be easily known.
  2. Detect the user touch using touchesBegan and touchesMoved. While the user touch moves, move the maskView as well (change maskView's frame). In this step, we need to decide that which vertex of the maskView is the closest to the Point that user touched -- leftTop, rightTop, leftBottom or rightBottom.
  3. On touchesEnded, we use the maskView's frame to crop the image. Here, the coordinates is exact enough. Notice that we need to change the maskView's frame for image's coordinate system.

Hope helps. :)

Community
  • 1
  • 1
Jason Lee
  • 3,200
  • 1
  • 34
  • 71
  • thats a more mature approach, I agree. I think I will have to do it your way sooner or later... but my point here is... ain't there any possible way of finding out the touch points exactly? (P.S:- I also thought of adding four buttons on the image to mark the four corners of the crop and have the user drag along these buttons. But first I need to see if I can do it simply by getting the touch points) – metsburg Feb 06 '13 at 09:27