Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIImageView in my UITableViewCell changes size on click

Tags:

I have a fairly vanilla UITableView in my app used to display an image and text in the default/standard way. The first time a UITableViewCell is requested I return it with a placeholder image and start an asynchronous download of the real image. When the download is complete I replace the placeholder image.

If a download fails, the placeholder image remains. Clicking on such a row acts normally. If the download is successful, clicking on a row with the intended image leads to the UIImageView expanding to the height of the row (and the width increases at scale). The UIImageView never returns to normal size. Crude attempts to set the frame of the UIImageView do not alleviate the issue of mysterious resizing.

// from my - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; method // after boilerplate cell dequeuing code cell.textLabel.text = [selectedNoun title];  // add placeholder image UIImage *placeholderImage = [UIImage imageNamed:@"placeholder.png"]; cell.imageView.image = placeholderImage; [cell.imageView setContentMode:UIViewContentModeScaleAspectFit]; //... // If the image has been downloaded I set it cell.imageView.image = iconDownloader.image; 

This is driving me nuts. It actually only happens in one of two tables in my app, but after comparing them line or line I can't see any difference. Hoping that someone has come across this before.

Thanks!

EDIT: I don't have a good explanation for my solution other than to say that images over a certain size appear to lead to this behavior and the use of actual thumbnails (even images somewhat bigger than the UIImageView's frame) do not exhibit this behavior.

like image 858
Jon Whitmore Avatar asked Dec 20 '11 20:12

Jon Whitmore


2 Answers

If you have custom UITableViewCell then make sure that your UIImageView is not named with "imageView"

like image 97
kalpeshdeo Avatar answered Sep 28 '22 01:09

kalpeshdeo


The only things that come to mind that effect the size & appearance of a view within its superview are:

  1. Are you changing the frame/bounds/center properties anywhere? Where/how is the frame set initially?

  2. imageView.autoResizingMask should be set to UIViewAutoResizingNone

  3. imageView.clipsToBounds should be set to YES

EDIT: more suggestions

I'm shooting in the dark b/c your posted code looks fine and if you've set imageView.clipsToBounds that should constrain the drawn image to the imageView frame. Here are a couple more things to try:

  1. Implement tableView:willDisplayCell:forRowAtIndexPath: and set the imageView properties there. If imageView.frame is the problem, this likely won't fix it.

  2. Add your own UIImageView to the cell configured how you want it and don't use the built in imageView property. If the behavior of the default cell is causing the problem this should work.

EDIT: large image problems

I don't know exactly how big the "really big images" are that you are currently using but they are likely the issue. From the UIImage docs (emphasis is mine):

You should avoid creating UIImage objects that are greater than 1024 x 1024 in size. Besides the large amount of memory such an image would consume, you may run into problems when using the image as a texture in OpenGL ES or when drawing the image to a view or layer. This size restriction does not apply if you are performing code-based manipulations, such as resizing an image larger than 1024 x 1024 pixels by drawing it to a bitmap-backed graphics context. In fact, you may need to resize an image in this manner (or break it into several smaller images) in order to draw it to one of your views.

like image 37
XJones Avatar answered Sep 28 '22 00:09

XJones