Resize the Table cell height as per downloaded image height

前端 未结 6 890
一生所求
一生所求 2021-01-05 07:04

I am getting the image\'s URL at run time and I want to download & display these images in a table. Images are going to be download asynchronously. What is more importan

6条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-05 07:39

    You can make your UIImageView size as per downloading image but it will give bad look.

    So i suggest you to make all your image of same size , so it will be look fine and sweet

    you can use this to make all your image of same size.

    + (UIImage *)imageScaledToSizeWithImage:(UIImage *)imagewww andsizeee:(CGSize)size
    {
        //avoid redundant drawing
        if (CGSizeEqualToSize(imagewww.size, size))
        {
            return imagewww;
        }
    
        //create drawing context
        UIGraphicsBeginImageContextWithOptions(size, NO, 0.0f);
    
        //draw
        [imagewww drawInRect:CGRectMake(0.0f, 0.0f, size.width, size.height)];
    
        //capture resultant image
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    
        //return image
        return image;
    }
    

    But if you really want to show table with different row size then make change your row size on run time

    when you will get image then save your image in dictionary with key value is indexPath of row.

    [tableImageDict setObject:image forKey:[NSString stringWithFormat:@"%i,%i",indexPath.row,indexPath.section]];
    

    and then reload your table row.

    [table reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                         withRowAnimation:UITableViewRowAnimationNone];
    

    It will change your row height

    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UIImage *image = (UIImage *)[tableImageDict objectForKey:
                                     [NSString stringWithFormat:@"%i,%i",
                                      indexPath.row,indexPath.section]];
    
    
        if (image != nil)
        {
            return image.size.height;
        }
        else{
            return 44.0;
        }
    }
    

提交回复
热议问题