将实际图像坐标转换为适合宽高比?

时间:2018-06-28 06:43:24

标签: ios swift uiimage uibezierpath cgpoint

例如:用户正在选择发送给后端的1000 * 1000张图像来检测对象,因此我正在获取图像中存在的对象的坐标。

Like this :(
                {
            x = 385;
            y = 249;
        },
                {
            x = 448;
            y = 242;
        },
                {
            x = 454;
            y = 276;
        },
                {
            x = 391;
            y = 284;
        }
    );

我将图像视图设置为宽高比合适,因此图像的实际大小会有所不同,因此,我需要根据宽高比合适的尺寸来更改坐标以适合宽高比合适的尺寸图像。

这是我尝试过的代码。

var topLeftXOne = point[0]["x"] as! CGFloat
var topLeftYOne = point[0]["y"] as! CGFloat
var topRightXOne = point[1]["x"] as! CGFloat
var topRightYOne = point[1]["y"] as! CGFloat
var bottomtopRightXOne = point[2]["x"] as! CGFloat
var bottomRightYOne = point[2]["y"] as! CGFloat

 var bottomLeftXOne = point[3]["x"] as! CGFloat
 var bottomLeftYOne = point[3]["y"] as! CGFloat

  let pointsView = ALPDView(frame: CGRect.zero)
  let x: CGRect = AVMakeRect(aspectRatio: image.size, insideRect: pointsView.imageView.frame)

 let scaleX = image.size.width / x.width
 let scaleY = image.size.height / x.height

 topLeftXOne = topLeftXOne / scaleX
 topLeftYOne = topLeftYOne / scaleY

 topRightXOne = topRightXOne / scaleX
 topRightYOne = topRightYOne / scaleY

 bottomtopRightXOne = bottomtopRightXOne / scaleX
 bottomRightYOne = bottomRightYOne / scaleY

 bottomLeftXOne = bottomLeftXOne / scaleX
 bottomLeftYOne = bottomLeftYOne / scaleY

但是我没有得到正确的结果。

enter image description here

例如图片,我想更改坐标以适合长宽比图片的大小。

1 个答案:

答案 0 :(得分:0)

最后,我通过使用ratio方法实现了这一点。我采用了图像尺寸的高宽比,并根据比例数学进行了计算。

让x:CGRect = AVMakeRect(aspectRatio:image.size,insideRect:pointsView.imageView.frame)

缩放比例X = image.size.width / x.width

缩放比例Y = image.size.height / x.height

                        var topLeftXOne = point[0]["x"] as! CGFloat
                        var topLeftYOne = point[0]["y"] as! CGFloat

                        var topRightXOne = point[1]["x"] as! CGFloat
                        var topRightYOne = point[1]["y"] as! CGFloat

                        var bottomtopRightXOne = point[2]["x"] as! CGFloat
                        var bottomRightYOne = point[2]["y"] as! CGFloat

                        var bottomLeftXOne = point[3]["x"] as! CGFloat
                        var bottomLeftYOne = point[3]["y"] as! CGFloat

                        let pointsView = ALPDView(frame: CGRect.init(x: 0, y: 0, width:self.view.frame.size.width , height: self.view.frame.size.height))

                        let additionValueForX = x.origin.x + 20
                        let additionValueForY = x.origin.y - 40

                        topLeftXOne = topLeftXOne / scaleX + additionValueForX
                        topLeftYOne = topLeftYOne / scaleY + additionValueForY

                        topRightXOne = topRightXOne / scaleX + additionValueForX
                        topRightYOne = topRightYOne / scaleY + additionValueForY

                        bottomtopRightXOne = bottomtopRightXOne / scaleX + additionValueForX
                        bottomRightYOne = bottomRightYOne / scaleY + additionValueForY

                        bottomLeftXOne = bottomLeftXOne / scaleX + additionValueForX
                        bottomLeftYOne = bottomLeftYOne / scaleY + additionValueForY