How to find the pixel location in original image in ios

前端 未结 2 1815
伪装坚强ぢ
伪装坚强ぢ 2021-01-16 09:41

I have an image with size 480x360 and am displaying in UIImageview with frame (0, 0, 320, 568) with aspectfill.

W

相关标签:
2条回答
  • 2021-01-16 10:25

    You just calculate the proportions and apply them to touch location point.

    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);
    
    0 讨论(0)
  • 2021-01-16 10:43

    Finding the pixel location of the touch in 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);
    
    0 讨论(0)
提交回复
热议问题