4

In my custom tableViewCell, I have an imageView (not the tableViewCell's default imageView) which fills the entire cell. It is set up in IB (and by code) with the contentMode UIViewContentModeAspectFill, and it clips to bounds. Whenever I change the imageView's image to an image loaded with [UIImage imageNamed:...], it resizes and fits the imageView as wished and expected. However, when the image set is loaded with [UIImage imageWithData:...], the image is set, but not resized.

The code that loads the image with data itself is run in a background thread, and looks like this:

- (void)getImage:(NSString *)URL {
    NSError *error = nil;
    UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:URL] options:NSDataReadingMapped error:&error]];
    if (!error){
        [self.thumbView setImage:image];
        [self.thumbView setContentMode:UIViewContentModeScaleAspectFill];
    }
}

I have tried setting the image on the main thread, in layoutSubviews, but it always produces the same result – it doesn't resize to fill the imageView. I added the image (JPG) to the app bundle and set it programmatically with [UIImage imageNamed:...], which works, but the app has to get the data from a URL.

I also tried using the UIImageView+AFNetworking-class to set the image async instead of making my own thread, but that doesn't work either.

Why isn't the image respecting the UIImageView's contentMode when it's loaded from data? Any help is appreciated. Neither JPEGs nor PNGs are working.

Emil
  • 7,220
  • 17
  • 76
  • 135
  • @OliverAtkinson Is that any better, really? This snippet works perfectly, except that the image doesn't resize as it's supposed to. – Emil Jul 27 '13 at 14:27
  • It handles caching the images so you don't have to worry about slowing down your users experience with loading images. It will also obey any contentMode settings already set on the UIImageView. AFNetworking is probably the best out there. – Oliver Atkinson Jul 27 '13 at 14:56
  • It's odd that you're using a transition method. Those methods are meant to replace one view with another view. I would consider testing the image change without an animation. Then try the `-[UIView animateWithDuration:animations:]` method to animate the change to your image. See the `UIView` docs for more info. – Bored Astronaut Jul 27 '13 at 19:00
  • As written, I have tried it without the transition. The transition method is also the only animation that will work for this purpose! :) – Emil Jul 27 '13 at 19:19
  • @OliverAtkinson I tried using AFNetworking's expansion of UIImageView to set the image, but it's still not respecting the contentMode! How strange. – Emil Jul 27 '13 at 20:16

1 Answers1

1

If anyone else stumbles upon the same issue, here's what I did to fix it.

I made a new imageView in IB, removed the old one, and only changed the contentMode-value and clipsToBounds-value. In the tableViewCell.m init-code, I set the placeholder image, and voila – everything works like a charm.

Although I don't really know what caused it, it may have been the Clears Graphics Content checkmark's fault for being set (oops).

Emil
  • 7,220
  • 17
  • 76
  • 135