所以我有一个项目,我拍摄图像,显示它,根据你点击的位置,它会返回rgb值。
但是,iphone上的显示比图像分辨率小得多,因此图像会缩小。
我试图通过将UIImageView上的tapLocation的坐标分别乘以:image.x / imageview.x和image.y / imageview.y来环绕它。
但颜色仍然偏离。
我的代码:
@IBAction func imageTap(_ sender: UITapGestureRecognizer) {
if sender.state == .ended {
let location = sender.location(in: imageDisplay)
let widthFactor = image.size.width / imageDisplay.frame.width
let heightFactor = image.size.height / imageDisplay.frame.height
let scaledWidth = location.x * widthFactor
let scaledHeight = location.y * heightFactor
let scaledLocation = CGPoint(x: scaledWidth, y: scaledHeight)
let colorAtLocation = image.getPixelColor(pos: scaledLocation)
let rgbValues = colorAtLocation.rgb()
let rValue = rgbValues!.red
let gValue = rgbValues!.green
let bValue = rgbValues!.blue
redValue.text = "\(String(describing: rValue))"
greenValue.text = "\(String(describing: gValue))"
blueValue.text = "\(String(describing: bValue))"
colorViewer.backgroundColor = colorAtLocation
}
}
我应该如何正确计算坐标?
可能出错的地方: