I'd like to display in (real world) mm/cm the distance (only on the y axis) between 2 images that can travel on the y axis. I've thought long and hard about this but can't think of a reasonable way to this given my inexperience (go easy on me! I'm new to development). If someone could help me out, that would be fantastic.
Asked
Active
Viewed 1,320 times
0
-
1Duplicate question:- http://stackoverflow.com/questions/610193/calculating-pixel-size-on-an-iphone – Tirth Jun 04 '13 at 07:18
-
You'll need to know the DPI of the device's display for one thing: http://stackoverflow.com/questions/13178186/how-can-i-detect-the-dpi-on-an-ipad-mini – trojanfoe Jun 04 '13 at 07:18
1 Answers
2
1 cm = 47 pixels and it is easy to find distance between 2 images in pixels..
so you can then convert pixels into centimeter ,like you have 200 pixels of distance then your centimeter will be like ---> (200/ 47) = 4.25 centimeter
int distance = imageview1.frame.origin.y - imageview2.frame.origin.y;
int pixeltoCentrimeter = distance/47;
Happy Coding!!!

NiravPatel
- 3,260
- 2
- 21
- 31
-
You say it's easy to find the distance between 2 images in pixels. I've never done it before. I had a quick search but found little that I understood. Could you demonstrate? Thanks – Invalid Memory Jun 04 '13 at 07:24
-
of course, I've just started trying the code in my project and will accept your answer when I've done that. ;) – Invalid Memory Jun 04 '13 at 08:31
-
sorry for taking so long, but I'm having a problem. I've used this code: `self.distance = self.laserGuide1.frame.origin.y - self.laserGuide2.frame.origin.y; self.pixeltoCentimeter = self.distance/47; NSString *intString = [NSString stringWithFormat:@"%d", self.pixeltoCentimeter]; self.cmDistance.text = intString;` but the label isn't showing anything. Is there anything you can see that I'm doing wrong? And don't worry, I'll accept your answer when its working, i wont forget ;) – Invalid Memory Jun 04 '13 at 08:54
-
no, there is nothing in the console. Just blank. The code is in the ViewDidLoad section for now. – Invalid Memory Jun 04 '13 at 09:54
-
First tell me that you are getting origin.y of both Imageview , first do NSLog of of origin.y of both imageviews.. – NiravPatel Jun 04 '13 at 10:11
-
I tried putting this in: `NSLog(@"pixeltoCentimeter %d", self.pixeltoCentimeter);` and it gave a reading in the console however using this code: `NSString *intString = [NSString stringWithFormat:@"%d", self.pixeltoCentimeter]; self.cmDistance.text = intString;` I get nothing in the console or in the label. Whats wrong with it?! or me! – Invalid Memory Jun 04 '13 at 10:51
-
just NSLog y -points of your both imageviews..like NSLOG(@"%f",self.laserGuide1.frame.origin.y); – NiravPatel Jun 04 '13 at 11:00
-
I get this: _2013-06-04 16:21:39.465 Blue Ruler Pro[7385:907] laserGuide1 is 54.000000 2013-06-04 16:21:39.466 Blue Ruler Pro[7385:907] laserGuide 2 is 350.000000 2013-06-04 16:21:39.467 Blue Ruler_ – Invalid Memory Jun 04 '13 at 15:22
-
Argh! I've found my mistake! There was nothing wrong with the code, I've been a plonker and didn't notice that the label had been automatically resized to around a pixel in height and width. I feel a little silly now... Thanks for your help NiravPatel! – Invalid Memory Jun 05 '13 at 07:31