Search code examples
iphoneios4uiimageviewuiimage

How to get coordinates of a view according to the coordinates of the uiimage on which it is placed in ios sdk?


I have an app where user takes a picture from camera and it is placed on a full screen image view. Now user can dear rectangular /square views on it and send the coordinates/size of the overlayed view to the server. The problem is i am getting the coordinates of the view according to the screen size but i want that the size should be according to the image on the imageview because normally image from the camera would be of larger resolution than the screen .

Please provide me a way to get the the size of the overlayed view according to the uiimage and not the screen

EDIT : For example :If user draw a view on a human face in the image and send the coordinates of the crop view to the server . Then it will be difficult to synchronize the coordinates with the real image if the coordinates are according to the screen and not according to the uiimage itself


Solution

  • The answer is simple:

    You have the frame of your UIImageView, and the frame of your drew square (both relative to the self.view)

    You only need to find the origin of your square, relative to the UIImageView.

    Just subtract:

    //get the frame of the square
    CGRect *correctFrame = square.frame;
    
    //edit square's origin
    correctFrame.origin.x -= imageView.frame.origin.x;
    correctFrame.origin.y -= imageView.frame.origin.y;
    

    Now correctFrame is the frame of your square relative to the imageView, while square.frame is still relative to self.view (as we didn't change it)

    To get the frame accordingly to the image resolution, do exact the same as above, then:

    float xCoefficient = imageResolutionWidth / imageView.frame.size.width;
    float yCoefficient = imageResolutionHeight / imageView.frame.size.height;
    
    correctFrame.origin.x *= xCoefficient;
    correctFrame.origin.y *= yCoefficient;
    correctFrame.size.width *= xCoefficient;
    correctFrame.size.height *= yCoefficient;
    

    Why: the image resolution is much grater than the imageView.frame, so you gotta calculate a coefficient that will adjust your square.

    That's all!