0

I have a UIImageView and when I set an image in that view using IB, the image is the correct size when I run the app (40x40). When I programmatically change the image using the code below, the new image is 400x400 or so. This is using the same image that I know works fine if it's set in IB. Do I have to scale the image before I add it to the UIImageView? I assumed auto-layout's constraints would automatically do this for me.

UIImage *messageTypeImage = [UIImage imageNamed:message.imageName];
[messageCell.imageView setImage:messageTypeImage];
[messageCell.contentView layoutIfNeeded];

The UIImageView's content mode is set to "Scale To Fill" and I've tried adding the code below both before and after calling setImage:

[messageCell.imageView setContentMode:UIViewContentModeScaleAspectFill];
[messageCell.imageView setClipsToBounds:YES];

Here's how the constraints are set up for the image view:

enter image description here

Chris Williams
  • 11,647
  • 15
  • 60
  • 97

2 Answers2

0

Are you using iOS 8 SDK? If so, try overriding layoutSubviews in your cell subclass and add this:

   self.contentView.frame = self.bounds;
   self.contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

There are some problems with the contentViews' of collection view/table view cells constraints/masks.

Horatiu Paraschiv
  • 1,750
  • 2
  • 15
  • 37
0

Wow. Finally found the answer here. Turns out, there's a default imageView outlet in prototype cells. I was unknowingly using that outlet and not the UIImageView I thought I was using.

Community
  • 1
  • 1
Chris Williams
  • 11,647
  • 15
  • 60
  • 97