iosobjective-cuitableviewuiimagecontentmode

UIImage not setting as AspectFill - iOS


I'm using an expandable TableView, (HVTableView), and in it I'm setting some images, which are downloaded from URLs. Problem is the images are not setting as AspectFill even though I've set everything in Storyboard as well as code.

Storyboard: enter image description here

code:

UIImageView *imageView = (UIImageView *)[cell viewWithTag:0];
if (object.imageFile) {
            
            
            [object.imageFile getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
                if (!error) {
                    UIImage *image = [UIImage imageWithData:data];
                    [imageView setClipsToBounds:YES];
                    [imageView setContentMode:UIViewContentModeScaleAspectFill];
                    [imageView setImage:image];
                }
                else [imageView setImage:[UIImage imageNamed:@"placeholder"]]; //initiL state
            }];
            
        }
        
        else {
            [imageView setImage:[UIImage imageNamed:@"placeholder"]];
        }

Result:

enter image description here

If I use local images, then they show perfectly, as square and aspectFill or whichever contentMode is set


Solution

  • Okay I just played a wild guess and tried subclassing UITableViewCell (made the UIImageView and UILabels as IBOutlets) and now it works! The images are perfectly behaving as aspectFill.

    I guess getting cell's contents and then setting items (as in the question) is not a smart way. Subclassing the solution!

    Thanks everyone for the help.