0

I'm working with a View-based application compiling for iPhoneOS 4.0 Simulator (Debug), Xcode 3.2.3.

I've got a UIImageView, imgView, whose center I want the coordinates of. I obtain them like this:

CGPoint imgviewcoords=[imgView center];

This doesn't produce any compile-time errors, but when I NSLog the coordinates like this:

NSLog(@"x: %i, y:%i", imgviewcoords.x, imgviewcoords.y);

I get this output:

x: 0, y:108762

It's showing 0 for imgView's x coordinate (which I know isn't right, because imgView is near the top-middle of the screen on Interface Builder) and some giant impossible number which is way past the boundaries of the iPhone's screen for the y coordinate (The y coordinate in the output above may not be exactly correct, but it's some giant number like that). I get this same exact output each time. The imgView is properly linked to its File's Owner outlet, and I can even change its image using

[imgview setImage:[UIImage imageNamed:@"./blahblah.png"]];

I just can't seem to properly get its center coordinates. I've also tried

CGPoint viewcoords=[[imgView frame] origin]; 

and that gives me the same erroneous coordinates in imgviewcoords as described above. This happens with every control that I have in my app's main UIView, except the y coordinate differs a little bit for each control. What am I doing wrong?

@Vladimir : Thanks for the suggestion to change the NSLog format specifiers. However, I don't think it's the output that is the problem. I think it's the [imgView center] call that isn't working. I'm using the CGPoint that's returned from [imgView Center] to set the center of another UIImageView, and that UIImageView simply moves to the very top-left of the screen instead of moving to the center of imgView. So I'm guessing it's the [imgView center] call that is returning a bad set of coordinates.

1 Answer 1

4

%i format specifier expects integer value and CGPoint components have CGFloat type, try to use correct specifier (%f) - may be you will get correct output:

NSLog(@"x: %f, y:%f", imgviewcoords.x, imgviewcoords.y);
2
  • Okay. I finally got it. When I got home I changed the format specifiers, and sure enough it gave me the right coordinates. Thanks! Feb 25, 2011 at 14:08
  • You can indicate that some answer is correct by clicking on the check box outline to the left of the answer. That will show that your problem is solved + will be a good reward for answerer ;)
    – Vladimir
    Feb 25, 2011 at 14:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.