1

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

Community
  • 1
  • 1
Hyder
  • 1,163
  • 2
  • 13
  • 37

2 Answers2

0

Updated based on comments below

Would you mind trying this?

UIImage *image = [UIImage imageWithData:data];
image = [UIImage imageWithCGImage:image.CGImage];

The problem is not the UIImageView but the Table View that is calculating the height of the the cells based on the size of the image.

Try overriding tableView:heightForRowAtIndexPath: in your table view controller and return a fixed value.

Community
  • 1
  • 1
Bell App Lab
  • 818
  • 6
  • 14
  • Not working. Infact that has already been overridden in my code `-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath isExpanded:(BOOL)isexpanded { if (isexpanded) return 200; return 80; }` – Hyder Dec 22 '15 at 12:20
  • Can you confirm that this method gets called? – Bell App Lab Dec 22 '15 at 12:21
  • Yes it is.. This method is the one which sets the row height when tapped, and its working fine.. The problem is with the images that are loaded from URL, they are showing as aspectFit.. – Hyder Dec 22 '15 at 12:38
  • It may be a wild guess, but would you mind taking a look at my edit? – Bell App Lab Dec 22 '15 at 12:52
0

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.

Hyder
  • 1,163
  • 2
  • 13
  • 37