Swift 如何在触摸屏上获取UIImage中特定像素的位置(或坐标)

Swift 如何在触摸屏上获取UIImage中特定像素的位置(或坐标),swift,uiimage,image-rendering,Swift,Uiimage,Image Rendering,在触摸图像时,我想获得该像素的坐标,我尝试了一些代码,但没有获得用户触摸图像的确切位置 这是我现有的代码 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { if let touch = touches.first { let position = touch.location(in: self) let Z1 = self.image!.

在触摸图像时,我想获得该像素的坐标,我尝试了一些代码,但没有获得用户触摸图像的确切位置

这是我现有的代码

 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)
    }
}
override func touchsbegind(touch:Set,带有事件:UIEvent?){
如果让触摸=先触摸{
让位置=触摸。位置(in:self)
设Z1=self.image!.size.height
设Z2=self.image!.size.width
设Z3=self.bounds.minY
设Z4=self.bounds.minX
设Z5=自限高度
设Z6=self.bounds.width
设pos1=(position.x-Z4)*Z2/Z6
设pos2=(position.y-Z3)*Z1/Z5
设点=CGPoint(x:pos1,y:pos2)
}
}

你的Touches开始被呼叫了吗?如果是这样的话,这就足够了

 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)
        }
    }
override func touchsbegind(touch:Set,带有事件:UIEvent?){
如果让触摸=先触摸{
let pos=触摸位置(在:图像视图中)
打印(位置x)
打印(位置y)
}
}

您能发布您尝试过的代码吗?让Z1=ImageView.image!。size.height让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=(位置y-Z3)*Z1/Z5让点=CG点(x:pos1,y:pos2)我试过用这段代码,你应该用这段代码来回答你的问题。而且,你似乎没有从任何地方得到任何用户输入。你是用
touchsbegind
方法还是类似的方法来做这件事的?在touchsbegind中放置一个断点,如果它被称为pleaseyes its,并给出了我的位置,但位置是有效的需要精确的像素位置试试看