Search code examples
iosswiftuiimageuibezierpathcgpoint

Convert real image coordinates to aspect fit?


Eg: user is choosing 1000 * 1000 image I am sending to backend to detect the object , so I am getting coordinates of the object that present in the image .

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

I am setting the image view to the aspect fit so the actual size of the image will vary , so based on the aspect fit size I need to change the coordinates to fit aspect fit size image .

This is the code that I was tried.

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

But I am not getting the proper result.

enter image description here

This the e.g. image , I want to change the coordinates to fit the aspect image size.


Solution

  • Finally I have achieved that by using ratio method . I took the aspect ratio of the image size and calculated according to ratio math.

    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

                            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