0

I have an image showed in uiimage view. If i touch some where in the screen, is there any method to know which point is touched in the image. i want to take the co-ordinates with respect to image, not screen co-ordinates. I want to do this without using any 3rd party library. Please suggest if there are any methods.

Shruthi
  • 45
  • 3

4 Answers4

1

try like this ,Important note is set userInteractionEnable=YES to your Image view by default user interaction is set to NO for image view.

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[event allTouches] anyObject];
    if([[touch view] isKindOfClass:[UIImageView  class]]){
        CGPoint point= [touch locationInView:touch.view];
        NSLog(@"%f%f",point.x,point.y);
    }

}
Balu
  • 8,470
  • 2
  • 24
  • 41
0

Once you get the relative coordinates from UIImageView touch event(and I am assuming you are using UIImageView and setting its Image property for Image). Then you can use the 1/UIImageView.image.scale to multiply with relative touch coordinates and get absolute coordinates for that Image.

Basically your original image gets skewed or scaled depending on the size of UIImageView so you need to do inverse of it.

TorukMakto
  • 2,066
  • 2
  • 24
  • 38
  • i have a scrollview associated with the image. When i zoom / scroll the above logic will not work na... – Shruthi Aug 26 '13 at 12:51
  • I have an scrollview above which i have placed an image. I am drawing lines above that image. I need to send the coordinates of the line with respect to original image(not imageview touch coordinates) to server. i have used the scrollview so that i can zoom image. – Shruthi Aug 27 '13 at 05:47
  • @Shruthi - Use the zoomScale property of UIScrollView along with scale property of UIImage.image. Combination of two should give you the right result. If the answer is helpful to you, please upvote or accept the answer so that it could be helpful to others in future too... – TorukMakto Aug 27 '13 at 06:52
0

Similar question is already asked and answered. Some of them pointed out here to help someone in future..

  1. https://stackoverflow.com/a/10212280/4140018
  2. https://stackoverflow.com/a/17200285/4140018
  3. http://b2cloud.com.au/tutorial/uiimageview-transforming-touch-coordinates-to-pixel-coordinates/
Community
  • 1
  • 1
Vineeth Joseph
  • 5,177
  • 3
  • 17
  • 31
-1

To convert point from one view to another view, you can use convertPoint:fromView: and convertPoint:toView: methods.

Prasad G
  • 6,702
  • 7
  • 42
  • 65