我的图片尺寸为480x360
,并显示在UIImageview
中,框架(0, 0, 320, 568)
显示为aspectfill
。
当我触摸图片视图时,位置显示为(150,120)
。如何以原始图片(480x360)
格式映射此位置?
由于
答案 0 :(得分:2)
您只需计算比例并将其应用于触摸位置点。
CGFloat _x = imageView.image.size.width / imageView.frame.size.width;
CGFloat _y = imageView.image.size.height / imageView.frame.size.height;
//assuming p - is CGPoint received from Touch event
CGPoint locationInImageView = CGPointMake(p.x * _x, p.y * _y);
答案 1 :(得分:0)
在Swift中查找触摸的像素位置:
guard let image = imageView.image else {
return
}
let x = image.size.width / imageView.frame.size.width
let y = image.size.height / imageView..frame.size.height
// Assuming touchLocation is CGPoint received from Touch event
var touchLocation: CGPoint
let locationInImageView = CGPoint(x: touchLocation.x * x, y: touchLocation.y * y);