I'm able to determine the coordinates of a touch event on a UIImageView. I need to then convert these coordinates to the coordinates of the image being displayed - which should be different, since my image is much larger than the UIImageView and I'm scaling the image with AspectFit.
What's the best way to determine the image coordinates of a touch event given the view coordinates of a UIImageView?
UIImage contains the data for an image. UIImageView is a custom view meant to display the UIImage .
UIImage *img = [[UIImage alloc] init]; and when you want to change the image: img = [UIImage imageNamed:@"nameOfPng. png"];
Something like this should get you going.
CGPoint imageViewPoint = [touch locationInView:imageView];
float percentX = imageViewPoint.x / imageView.frame.size.width;
float percentY = imageViewPoint.y / imageView.frame.size.height;
CGPoint imagePoint = CGPointMake(image.size.width * percentX, image.size.height * percentY);
Assuming the UIImageView
is called imageView
the UITouch
is called touch
and the UIImage
is called image
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With