Getting the height, width, and origin of a view!

Member
Posts: 22
Joined: 2009.08
Post: #1
Hey all. I'm just starting out on the iPhone and I've got a flurry of questions that I'm sure have very easy answers. I've given it a shot, but it's time to ask:

Let's say "image" is an IBOutlet for a UIImageView. I'd like to get the width, height, and origin of the image. How is this done?

I tried image.width and image.height, but this doesn't work. I know that the width and height are stored within a CGSize and all that business, but I'm not sure how to extract that information. Same with the origin.

Thanks a ton!
Sam
Quote this message in a reply
Member
Posts: 245
Joined: 2005.11
Post: #2
How to Find This Out Yourself

1 - Read the developer documentation for UIImageView.
2 - The answer isn't in there, but it does say that UIImageView is a subclass of UIView.
3 - Look at the documentation for UIView which supports the following method:
Quote:bounds
The receiver’s bounds rectangle, which expresses its location and size in its own coordinate system.

@property(nonatomic) CGRect bounds
Discussion
The bounds rectangle determines the origin and scale in the view’s coordinate system within its frame rectangle. Setting this property changes the value of the frame property accordingly.

Changing the frame rectangle automatically redisplay the receiver without invoking the drawRect: method. If you want the drawRect: method invoked when the frame rectangle changes, set the contentMode property to UIViewContentModeRedraw.

Changes to this property can be animated. Use the beginAnimations:context: class method to begin and the commitAnimations class method to end an animation block.

The default bounds origin is (0,0) and the size is the same as the frame rectangle’s size.

Availability
Available in iPhone OS 2.0 and later.
See Also
@property frame
@property center
@property transform
Related Sample Code
avTouch
QuartzDemo
SpeakHere
TheElements
UICatalog
Declared In
UIView.h

Depending on what you are actually hoping to accomplish, it is possible that the frame property (in "see also") is what you are actually looking for.
Quote this message in a reply
Apprentice
Posts: 10
Joined: 2009.08
Post: #3
It also says in the UIImageView that it contains a UIImage property. You can also look at the developer documentation for UIImage which has the following property.

Quote:size

The dimensions of the image, taking orientation into account (read-only)

@property(nonatomic, readonly) CGSize size

Availability

* Available in iPhone OS 2.0 and later.

Related Sample Code

* Accessory
* BubbleLevel
* iPhoneCoreDataRecipes
* MoveMe
* TouchCells

Declared In
UIImage.h

So your close, just not pulling the right property out of image. If your not sure how to get the width and height out of a CGSize, there is also developer documentation on that, or just look at the header files.

For the origin, you have to follow what backslash said. The position of the image is controlled by the view. Pay attention to the See Also section of the documentation. In your case for example, in the bounds "See Also"

Quote:@property center
Quote this message in a reply
Member
Posts: 22
Joined: 2009.08
Post: #4
Thanks for the speedy response!

I actually had already found that, but maybe I went too far. I went into the frame section, then when I said before it's stored in a CGSize, that's what I found. I know how to navigate the Document, but I'm not sure how to return the information into a variable.

myImageView.width doesn't work as "width" is not recognized. Even if I have a variable that's linked to a CGWhatever that represents height and width, how do I get each one separated into it's own variable?

Thanks again.
Quote this message in a reply
Apprentice
Posts: 10
Joined: 2009.08
Post: #5
Yea that's why I pointed you to the reference of size. You have to get the size from the image, then you can get the width or height from the size. So something like this should do it.

Code:
CGFloat width = [imageView image].size.width;
CGFloat height = [imageView image].size.height;
Quote this message in a reply
Member
Posts: 22
Joined: 2009.08
Post: #6
Oh, there it is! I wasn't doing the double dot operator. I guess I was assuming that the properties were inherited as you move up from the width<size<frame<blahblahblah .

Last follow-up for this newbie.

So then why does
myImage.center work but not myImage.width?
Quote this message in a reply
Apprentice
Posts: 10
Joined: 2009.08
Post: #7
If you look at the documentation, center is a property of UIImageView making myImageView.center valid. However, width is not. Width is a value of size, which is a property of UIImage, which is a property of UIImageView.
Quote this message in a reply
Member
Posts: 22
Joined: 2009.08
Post: #8
Oh I see! Great, thanks a ton. Helps out a ton.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Determining UIScrollView indicator width charshep 0 2,946 Mar 12, 2009 11:19 AM
Last Post: charshep