0

On touching on the image I want to get coordinate of that pixel, I tried some code but not getting exact location where the user touches on the image.

This my existing code

 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    if let touch = touches.first {
        let position = touch.location(in: self)


        let Z1 = self.image!.size.height

        let Z2 = self.image!.size.width

        let Z3 = self.bounds.minY

        let Z4 = self.bounds.minX

        let Z5 = self.bounds.height

        let Z6 = self.bounds.width

        let pos1 = (position.x - Z4) * Z2 / Z6

        let pos2 = (position.y - Z3) * Z1 / Z5

        let point = CGPoint(x: pos1, y: pos2)
    }
}
3
  • Can you post the code that you have tried? Commented Aug 22, 2019 at 6:53
  • let Z1 = ImageView.image!.size.height let Z2 = ImageView.image!.size.width let Z3 = ImageView.bounds.minY let Z4 = ImageView.bounds.minX let Z5 = ImageView.bounds.height let Z6 = ImageView.bounds.width let pos1 = (position.x - Z4) * Z2 / Z6 let pos2 = (position.y - Z3) * Z1 / Z5 let point = CGPoint(x: pos1, y: pos2) i have tried with this code Commented Aug 22, 2019 at 6:55
  • You should edit your question with that code. Also, it doesn't seem like you are getting any user input from anywhere. Are you doing this in a touchesBegan method or something like that? Commented Aug 22, 2019 at 6:57

1 Answer 1

2

Is your touchesBegan getting called ? If so this should be enough..

 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        if let touch = touches.first {
            let pos = touch.location(in: imageView)
            print(pos.x)
            print(pos.y)
        }
    }
Sign up to request clarification or add additional context in comments.

4 Comments

Put a breakpoint in touchesBegan if it is getting called please
yes its and also giving me position, but position is working
i need exact pixel location
try looking here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.