2

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

Pankaj Kainthla
  • 2,439
  • 5
  • 31
  • 61
  • I am trying to understand: you take high resolution images with the camera and want to send resized (small) images to server? – John Smith Aug 16 '12 at 14:00
  • @Sava : No i want to send the coordinates of views (crop boxes) that user places on screen. The copy of tha same image is also on server.So 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 – Pankaj Kainthla Aug 16 '12 at 14:05
  • in this case, you need to post your code that draws the rect of the face, let's see how you do this. I guess the problem is there – John Smith Aug 16 '12 at 14:09
  • @Sava: I just place a rectangular UIview on the screen.(as a subview of the uiimageview) User can then drag or resixe it like a crop view than send its x,y width,height to server. – Pankaj Kainthla Aug 16 '12 at 14:13
  • I have posted the answer and made some edits, see, now it has to work just fine – John Smith Aug 16 '12 at 14:30

1 Answers1

2

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!

John Smith
  • 2,012
  • 1
  • 21
  • 33
  • I want to have coordinates according to the image resolution and not the imageview. For exampla if i have an image of resolution (1100,1500) and i drew a full screen crop view then the crop view's coordinates will be (0,0 768,1024) according to the imageview .If i send (0,0768,1024) to the server then it will be wrong as i want it to be (1100,1500) otherwise the results will be wrong – Pankaj Kainthla Aug 16 '12 at 14:38
  • Edits have been made, take a look – John Smith Aug 16 '12 at 14:51
  • Thats a greate logic . It seems to be working like a charm . I will test it in real time conditions and report back .. Thanks a lot . Great work – Pankaj Kainthla Aug 16 '12 at 15:42
  • There is one more problem . I will send the changed/ modified coordinates to the server and store them . But user have a choice to see their previous work . So in this case i will get back the coordinates from the server and create pre defined view according to the stored coordinates from server.But now the crop view will be changed becsause i stored modified coordinated. Is there any way to know the original coordinates i.e correct.frame.xyx ?? – Pankaj Kainthla Aug 16 '12 at 15:48
  • Well i can store both the original and mofified coordinates to the server and use the original for the future preview ? But can it be done with an algorithm like above but i think for that i also have to store the image resolution on server – Pankaj Kainthla Aug 16 '12 at 15:51
  • yes, you'll have to store both parameters. in case all your image have the same resolution, and the imageview you are presenting the image will alway be the same size, you can only store the coefficients in your application and use them. but storing 2 parameters is not a problem, do like this. – John Smith Aug 16 '12 at 17:50