4

I am using SDWebImage in my iPhone project and I use it to load images to my table cell.

The original downloaded image 150 * 150. My placeholder image is 32*32 (required size)

First time, when the image loads, sdwebimage does a great job of re-sizing the downloaded image to match the size of the placeholder image (32*32). All good.

However, when I navigate back and then come back to the same page, the image gets stretched and fills the entire cell.imageView height. Is this normal behavior?

I need it to always retain the initial 32*32 size. Can you suggest how to do that?

EDIT: I found this link where another user faces a similar issue (question open). The user also made a short youtube video to explain the issue.

Thoughts?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Ravi
  • 3,719
  • 6
  • 28
  • 40

4 Answers4

3

tldr: If your custom UITableViewCell subclass has a @property IBOutlet UIImageView* called imageView, change your name to something else (such as customImageView) and it will work properly. Read the rest of the answer for why

I see nobody has answered that, and I wasted a whole week trying to find the solution to this, so here's what happened to me, and how to prevent it happening to you (thus fixing your problem).

I created a CustomTableViewCell which extends UITableViewCell.

I added @property (weak, nonatomic) IBOutlet UIImageView *imageView; to my custom cell. Everything seems fine until now, right?

Except it is not.

UITableViewCell already has an imageView property. iOS 7 uses your property at first, but then when the Cell is being reused, the UITableViewCell's imageView is used, therefore ignoring all of your constraints or previously defined fixed sizes.

iOS 8 always uses the image from UITableViewCell. so the problem is more obvious there. Previous iOS versions also had varying degrees of this problem, usually presenting very similar symptoms.

The solution? Either deal with the property UITableViewCell.imageView by yourself (add code for laying out the view) or create your own property with a different name, with which you can add constraints (in case you're using auto layout) on Storyboards or xib.

Velociround
  • 611
  • 7
  • 18
  • 1
    My god, you can't believe how much health you gave me with your answer, I could either feel stupid or thankful, either way if it wasn't for your answer I would have probably ended up doing something complicated such as setting the location of the imageview in code or something like that. Thanks a ton!! – Alan Poggetti Sep 18 '15 at 10:06
  • Ugh.. same. This should be a warning generated. Sad thing is I think I just did the textLabel (same type of problem) two weeks ago and caught myself. – Evan Anger Dec 09 '17 at 17:00
3

try that for specific size at the cellForRowAtIndexPath :

   UIImageView *addImage = [[UIImageView alloc] init];
   [addImage setImageWithURL:[NSURL URLWithString:yourCellImageURL] placeholderImage:[UIImage imageNamed:@"placeholder.png"]]; 
   [addImage setFrame:CGRectMake(7, 3, 78, 57)];
   [cell.contentView addSubview:addImage];
   [cell setIndentationLevel:8];
mturhan55
  • 211
  • 3
  • 9
0

if you want to change image size in SDWebImage. use this call.

// Here we use the new provided setImageWithURL: method to load the web image
[cell.imageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"]
           placeholderImage:[UIImage imageNamed:@"placeholder.png"]
                  completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) { /*here you can change size of image */ }];
laalto
  • 150,114
  • 66
  • 286
  • 303
Saif
  • 812
  • 1
  • 9
  • 18
0

Try to set proper content mode of the UIImageView

yebw
  • 247
  • 1
  • 3
  • 11
  • Thanks for the response. Can you please elaborate? I tried UIViewContentModeScaleAspectFit and it had no effect. I do not have a custom table cell. – Ravi Feb 01 '12 at 04:25