Swift 如何确定用户在UIImage上点击的像素(x,y)?

Swift 如何确定用户在UIImage上点击的像素(x,y)?,swift,image,Swift,Image,我有一个UIImageView。我添加一个图像并点击图像的某些位置 有没有办法确定抽头图像的像素(x,y)是多少 考试: 如果我有apple.png,图像大小为x:1000,y:1000,我点击它的正中央,它应该返回x:500,y:500 我需要真实图像上的点击点像素(UIImageView.image)而不是UIImageView是,通过向图像视图添加UIAPTgestureRecognitizer,从中获取点击位置并将点击坐标转换为图像坐标: let img = UIImage(named:

我有一个
UIImageView
。我添加一个图像并点击图像的某些位置

有没有办法确定抽头图像的像素(x,y)是多少

考试:

如果我有
apple.png
,图像大小为
x:1000,y:1000
,我点击它的正中央,它应该返回
x:500,y:500


我需要真实图像上的点击点像素(
UIImageView.image
)而不是
UIImageView
是,通过向图像视图添加
UIAPTgestureRecognitizer
,从中获取点击位置并将点击坐标转换为图像坐标:

let img = UIImage(named: "whatever")

// add a tap recognizer to the image view
let tap = UITapGestureRecognizer(target: self, 
             action: #selector(self.tapGesture(_:)))
imgView.addGestureRecognizer(tap)
imgView.isUserInteractionEnabled = true
imgView.image = img
imgView.contentMode = .scaledAspectFit

func convertTapToImg(_ point: CGPoint) -> CGPoint? {
    let xRatio = imgView.frame.width / img.size.width
    let yRatio = imgView.frame.height / img.size.height
    let ratio = min(xRatio, yRatio)

    let imgWidth = img.size.width * ratio
    let imgHeight = img.size.height * ratio

    var tap = point
    var borderWidth: CGFloat = 0
    var borderHeight: CGFloat = 0
    // detect border
    if ratio == yRatio {
        // border is left and right
        borderWidth = (imgView.frame.size.width - imgWidth) / 2
        if point.x < borderWidth || point.x > borderWidth + imgWidth {
            return nil
        }
        tap.x -= borderWidth
    } else {
        // border is top and bottom
        borderHeight = (imgView.frame.size.height - imgHeight) / 2
        if point.y < borderHeight || point.y > borderHeight + imgHeight {
            return nil
        }
        tap.y -= borderHeight
    }

    let xScale = tap.x / (imgView.frame.width - 2 * borderWidth)
    let yScale = tap.y / (imgView.frame.height - 2 * borderHeight)
    let pixelX = img.size.width * xScale
    let pixelY = img.size.height * yScale
    return CGPoint(x: pixelX, y: pixelY)
}

@objc func tapGesture(_ gesture: UITapGestureRecognizer) {
    let point = gesture.location(in: imgView)
    let imgPoint = convertTapToImg(point)
    print("tap: \(point) -> img \(imgPoint)")
}
让img=UIImage(名为“whatever”)
//将点击识别器添加到图像视图中
让tap=UITAPPgestureRecognitor(目标:自我,
操作:#选择器(self.tappostate(#:))
imgView.AddGestureRecognitor(点击)
imgView.isUserInteractionEnabled=true
imgView.image=img
imgView.contentMode=.ScaledSpectFit
func ConvertTaptomg(upoint:CGPoint)->CGPoint?{
设xRatio=imgView.frame.width/img.size.width
让yRatio=imgView.frame.height/img.size.height
让压比=最小值(X比,yRatio)
设imgWidth=img.size.width*比值
让imgHeight=img.size.height*比值
var tap=点
变量borderWidth:CGFloat=0
变量边界高度:CGFloat=0
//检测边界
如果比率==yRatio{
//边界是左右的
borderWidth=(imgView.frame.size.width-imgWidth)/2
如果点.xborderWidth+imgWidth{
归零
}
点击.x-=边框宽度
}否则{
//边界是顶部和底部
边框高度=(imgView.frame.size.height-imgHeight)/2
如果点.yborderHeight+imghight{
归零
}
tap.y-=边框高度
}
设xScale=tap.x/(imgView.frame.width-2*borderWidth)
设yScale=tap.y/(imgView.frame.height-2*边框高度)
设pixelX=img.size.width*xScale
设像素=img.size.height*yScale
返回点(x:pixelX,y:pixelY)
}
@objc func TAPPORATION(uu手势:UITAPPESTURE识别器){
让点=手势位置(在:imgView中)
设imgPoint=convertTapToImg(点)
打印(“点击:\(点)->img\(imgPoint)”)
}

是,通过向图像视图添加一个
UITapGestureRecognitor
,从中获取点击位置,并将点击坐标转换为图像坐标:

let img = UIImage(named: "whatever")

// add a tap recognizer to the image view
let tap = UITapGestureRecognizer(target: self, 
             action: #selector(self.tapGesture(_:)))
imgView.addGestureRecognizer(tap)
imgView.isUserInteractionEnabled = true
imgView.image = img
imgView.contentMode = .scaledAspectFit

func convertTapToImg(_ point: CGPoint) -> CGPoint? {
    let xRatio = imgView.frame.width / img.size.width
    let yRatio = imgView.frame.height / img.size.height
    let ratio = min(xRatio, yRatio)

    let imgWidth = img.size.width * ratio
    let imgHeight = img.size.height * ratio

    var tap = point
    var borderWidth: CGFloat = 0
    var borderHeight: CGFloat = 0
    // detect border
    if ratio == yRatio {
        // border is left and right
        borderWidth = (imgView.frame.size.width - imgWidth) / 2
        if point.x < borderWidth || point.x > borderWidth + imgWidth {
            return nil
        }
        tap.x -= borderWidth
    } else {
        // border is top and bottom
        borderHeight = (imgView.frame.size.height - imgHeight) / 2
        if point.y < borderHeight || point.y > borderHeight + imgHeight {
            return nil
        }
        tap.y -= borderHeight
    }

    let xScale = tap.x / (imgView.frame.width - 2 * borderWidth)
    let yScale = tap.y / (imgView.frame.height - 2 * borderHeight)
    let pixelX = img.size.width * xScale
    let pixelY = img.size.height * yScale
    return CGPoint(x: pixelX, y: pixelY)
}

@objc func tapGesture(_ gesture: UITapGestureRecognizer) {
    let point = gesture.location(in: imgView)
    let imgPoint = convertTapToImg(point)
    print("tap: \(point) -> img \(imgPoint)")
}
让img=UIImage(名为“whatever”)
//将点击识别器添加到图像视图中
让tap=UITAPPgestureRecognitor(目标:自我,
操作:#选择器(self.tappostate(#:))
imgView.AddGestureRecognitor(点击)
imgView.isUserInteractionEnabled=true
imgView.image=img
imgView.contentMode=.ScaledSpectFit
func ConvertTaptomg(upoint:CGPoint)->CGPoint?{
设xRatio=imgView.frame.width/img.size.width
让yRatio=imgView.frame.height/img.size.height
让压比=最小值(X比,yRatio)
设imgWidth=img.size.width*比值
让imgHeight=img.size.height*比值
var tap=点
变量borderWidth:CGFloat=0
变量边界高度:CGFloat=0
//检测边界
如果比率==yRatio{
//边界是左右的
borderWidth=(imgView.frame.size.width-imgWidth)/2
如果点.xborderWidth+imgWidth{
归零
}
点击.x-=边框宽度
}否则{
//边界是顶部和底部
边框高度=(imgView.frame.size.height-imgHeight)/2
如果点.yborderHeight+imghight{
归零
}
tap.y-=边框高度
}
设xScale=tap.x/(imgView.frame.width-2*borderWidth)
设yScale=tap.y/(imgView.frame.height-2*边框高度)
设pixelX=img.size.width*xScale
设像素=img.size.height*yScale
返回点(x:pixelX,y:pixelY)
}
@objc func TAPPORATION(uu手势:UITAPPESTURE识别器){
让点=手势位置(在:imgView中)
设imgPoint=convertTapToImg(点)
打印(“点击:\(点)->img\(imgPoint)”)
}

我想OP想要的是
UIImage.size
的像素位置,而不是
UIImageView
的点值。谢谢@Gereon,但我想要的是
UIImage
上点击点的像素位置。不是
UIImageView
。当你有a)点击位置,b)UIImage的大小,c)UIImageView的框架和d)屏幕的比例因子时,计算不是很简单吗?@Gereon,如果你知道如何计算它,请告诉我。更新了我的答案。我想OP需要
UIImage的像素位置。大小
,不是
UIImageView
的点值。谢谢@Gereon,但我想要
UIImage
上点击点的像素位置。不是
UIImageView
。当你有a)点击位置,b)UIImageView的大小,c)UIImageView的框架和d)屏幕的比例因子时,计算不是很简单吗?@Gereon,如果你知道如何计算它,请告诉我。更新了我的答案你在使用
。scaleSpectfit
?如果是这样,您需要有三件事:(1)点击的点位置,(2)显示图像的实际
cgrit
——注意,不是图像视图的
cgrit
,最后(3)在两者之间转换的计算/公式。我有一个
UIImage
扩展,可以帮助您处理第二个问题。它返回图像视图对图像的渲染的“比例因子”,无论其大小是向上还是向下调整。我只是不确定它是否能与其他
.contentModes
一起正常工作。是的@dfd,我使用
scaleAspectFit
。我已经有了点击点的位置,但它是基于屏幕的x,y。再仔细想想,我最多只能让你们到达一半。我的分机会给你的