質問

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.

役に立ちましたか?

解決

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).

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top