0

I have a custom tableview and under each cell i have image-view. I am receiving images of different sizes from the service and i want to adjust my frame according to that.

Whenever there is a picture smaller than the frame size i want to adjust the height according to its height.

enter image description here

So i am comparing the current frame size and image size and trying to update the imageview frame,but its not working.

cell.image.contentMode = UIViewContentModeScaleAspectFit;
        if(cell.image.image.size.height<cell.image.frame.size.height)
        {
        cell.image.frame = CGRectMake(cell.image.frame.origin.x, cell.image.frame.origin.y,cell.image.frame.size.width, cell.image.image.size.height);
        }

what needs to be done to overcome the problem??

Fay007
  • 2,707
  • 3
  • 28
  • 58

2 Answers2

0

If the code above is in cellForRowAtIndexPath and the cell that you have initialized inside cellForRowAtIndexPath is an instance of ItemCell (for instance), then in your ItemCell class, you will have to override layoutSubviews and check the size of the image subview inside layoutSubviews and adjust the height of the frame of the cell accordingly inside layoutSubviews. This ensures that every time for cellForRowAtIndexPath is called, the layout of the UITableViewCell subclass (ItemCell) happens before the cell gets rendered by cellForRowAtIndexPath.

Btw, the fact that you have set the contentMode of the cell to ScaleAspectFit means that regardless of what you do to the frame of the imageView inside the cell, the image will always be scaled to fit the entire view available to it, i.e. in this case, the dimensions of the imageView inside the cell. Maybe you should try moving the contentMode line

 cell.image.contentMode = UIViewContentModeScaleAspectFit;

after the conditional where you set the frame size of the imageView :

    if(cell.image.image.size.height<cell.image.frame.size.height)
    {
    cell.image.frame = CGRectMake(cell.image.frame.origin.x, cell.image.frame.origin.y,cell.image.frame.size.width, cell.image.image.size.height);
    }
cell.image.contentMode = UIViewContentModeScaleAspectFit;

Ofcourse, this would probably not solve the problem if you aren't overriding layoutSubview to adjust the dimensions of the imageView frame there.

Arunabh Das
  • 13,212
  • 21
  • 86
  • 109
  • yes its inside cellForRowAtlndexPath, can u please let me know how to override that layoutSubviews?? @Das – Fay007 Mar 28 '16 at 09:22
0

You would require to change cell height according to image in UITableView datasource like this

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
  if (image.size.width > CGRectGetWidth(self.view.bounds)) {
    CGFloat ratio = image.size.height / image.size.width;
    return CGRectGetWidth(self.view.bounds) * ratio;
  } else {
    return image.size.height;
  }
}

EDIT Incase you are looking out for resizing image and not tableview cell then these are Resize UIImage with aspect ratio? and Resize UIImage with aspect ratio? very good references. Also you should understand UIImageView Scaling Explained Visually

Community
  • 1
  • 1
aman.sood
  • 872
  • 7
  • 19
  • so i don't need to do anything under cellForRowAtIndexPath ?? @aman.sood – Fay007 Mar 28 '16 at 09:29
  • yes just set image to your UIImageView. Also check in custom cell xib what is the view -> mode set, it should be scale to fill – aman.sood Mar 28 '16 at 09:39
  • but declaring cell object in heightForRowAtIndexPath giving me exception – Fay007 Mar 28 '16 at 09:45
  • i have tried to add TimelineTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; under heightForRowAtIndexPath – Fay007 Mar 28 '16 at 09:48
  • sorry my bad. it should not be cell there as cell is still not in memory, I just copied your code. So you should have make image object available in heightForRowAtIndexPath – aman.sood Mar 28 '16 at 09:56
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/107523/discussion-between-aman-sood-and-fay007). – aman.sood Mar 28 '16 at 10:08
  • @Fay007 edit my answer incase you are looking out for image resizing. – aman.sood Mar 28 '16 at 11:34
  • i just added your code,after doing so my pics height is looking ok but the width became smaller!!! – Fay007 Mar 28 '16 at 13:09