Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting UIImageView Touch Coordinates to UIImage Coordinates

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?

like image 316
Bryan Porter Avatar asked Jun 19 '13 19:06

Bryan Porter


People also ask

What is the difference between a UIImage and a UIImageView?

UIImage contains the data for an image. UIImageView is a custom view meant to display the UIImage .

How do you declare UIImage in Objective C?

UIImage *img = [[UIImage alloc] init]; and when you want to change the image: img = [UIImage imageNamed:@"nameOfPng. png"];


1 Answers

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.

like image 57
Fogmeister Avatar answered Sep 27 '22 20:09

Fogmeister