将UIImageView触摸坐标转换为UIImage坐标

时间:2023-02-09 09:57:48

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.

我能够在UIImageView上确定触摸事件的坐标。我需要将这些坐标转换为正在显示的图像的坐标 - 这应该是不同的,因为我的图像比UIImageView大得多,我正在使用AspectFit缩放图像。

What's the best way to determine the image coordinates of a touch event given the view coordinates of a UIImageView?

在给定UIImageView的视图坐标的情况下,确定触摸事件的图像坐标的最佳方法是什么?

1 个解决方案

#1


12  

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.

假设UIImageView被称为imageView,UITouch称为触摸,UIImage称为图像。

#1


12  

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.

假设UIImageView被称为imageView,UITouch称为触摸,UIImage称为图像。