【问题标题】:How to find the pixel location in original image in ios如何在ios中找到原始图像中的像素位置
【发布时间】:2016-07-12 14:34:16
【问题描述】:

我有一个大小为480x360 的图像,并在UIImageview 中显示,框架为(0, 0, 320, 568)aspectfill

当我触摸图像视图时,位置显示(150,120)。如何以我的原始图像(480x360) 格式映射此位置?

谢谢

【问题讨论】:

    标签: ios uiimageview uitouch


    【解决方案1】:

    您只需计算比例并将它们应用于触摸位置点。

    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);
    

    【讨论】:

      【解决方案2】:

      在 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);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-12-30
        • 1970-01-01
        • 2010-11-29
        • 1970-01-01
        • 2023-03-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多