views:

210

answers:

3

I have a UITableView that displays single large images in each cell. The names of the images are stored in a plist. I would like to adjust the hight of each cell to accommodate the height of the image.

Does anyone know of a way to get the height for an image and use it to set the row height?

I'm having trouble finding this one and really appreciate any help.

+1  A: 

The best thing to do would probably be to pre-load the image heights into your plist. Otherwise, you're going to have to load the image (using UIImage's imageWithContentsOfFile: method), and then get its size (a property).

To set custom table row heights, implement -tableView:heightForRowAtIndexPath: in your table's delegate.

Ben Gottlieb
Thanks for the suggestions. Do you have any tips on preloading the image heights into the plist?Thanks!
Jonah
When you insert it into the plist, have another key that you set the height for, rather than at run time. Do it once when you insert it.
Garrett
+1  A: 

The property for the height of a UIImage object - let's call it image - is:

CGFloat imageHeight = image.size.height;
Tyler
A: 

Here is the code that actually worked. I put it under the heightForRowAtIndexPath method.

UIImage *imageForHeight = [UIImage  imageWithContentsOfFile:MyImagePath];   

imageHeight = CGImageGetHeight(imageForHeight.CGImage);

return imageHeight;

Just using:

CGFloat imageHeight = image.size.height;

as Jason recommended did not work for some reason. But he got me on the right track.

Jonah