UIImageView set to imageWithData ignores contentMode

In my custom tableViewCell, I have an imageView (and not a default imageViewView tableViewCell) that fills the whole cell. It is configured in IB (and by code) with contentMode UIViewContentModeAspectFill , and it is copied to frames. Whenever I change the imageView image to an image uploaded using [UIImage imageNamed:...] , it resizes and matches the image as desired and expected. However, when a set of images is uploaded using [UIImage imageWithData:...] , the image is set but not changed.

The code that loads the image with the data itself runs in the 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 tried to set the image in the main thread, in layoutSubviews , but it always gives the same result - it does not resize to fill the imageView. I added an image (JPG) to the application package and installed it programmatically using [UIImage imageNamed:...] , which works, but the application should get the data from the URL.

I also tried using the UIImageView + AFNetworking class to set the asynchronous image instead of creating my own stream, but this also does not work.

Why is the image related to the UIImageView contentMode not loading from the data? Any help is appreciated. Neither JPEG nor PNG work.

+4
source share
1 answer

If someone else stumbles on the same problem, here is what I did to fix it.

I created a new imageView in IB, deleted the old one and only changed the contentMode-value and clipsToBounds-value. In the initView code of tableViewCell.m I set the placeholder image, and voila - everything works like a charm.

Although I really don't know what caused this, it may have been a Checkmark error for Clears graphic content for installation (oops).

+1
source

Source: https://habr.com/ru/post/1493812/


All Articles