【问题标题】:Resize the Table cell height as per downloaded image height根据下载的图像高度调整表格单元格高度
【发布时间】:2012-04-22 22:55:42
【问题描述】:

我在运行时获取图像的 URL,我想下载并在表格中显示这些图像。图像将被异步下载。更重要的是,我想以实际尺寸显示所有这些图像。

请帮助我。在此先感谢:)

【问题讨论】:

  • 问你几个问题。这些图像是否会占用完整的 320px 宽度的屏幕?还是将它们显示在另一个下方,无论宽度如何?或者如果上一张图片的宽度小于 320,而下一张也可以适应剩余的宽度(320-prev_img_width),那么它会出现在上一张图片的旁边或上一张的下方。

标签: iphone ios uitableview uiimage


【解决方案1】:

查看heightForRowAtIndexPath:。这是另一个关于它的post

【讨论】:

    【解决方案2】:

    为此,您必须创建自定义 UITableviewCell,并且您可以使用此代码设置表格视图单元格的大小

    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
       int rowHeight =0.0f;
    
       // here u can get particular Url by indexPath.row, now create UIImage object using that Url 
    
       CGRect bioHeadingFrame = image.frame;
       rowHeight = size.height; //get the height of that image 
    
       return rowHeight;
    }
    

    现在您的表格单元格的高度根据图像高度增加

    【讨论】:

    • 你说的对。但是要在表格视图单元格中下载和显示图像,我使用的是异步视图类。我正在将图像 URL 传递给此类(异步视图类)以进行下载。只有在下载图像后,我才会得到实际的图像大小。那么我怎样才能告诉 tableview 重新加载和制作图像大小的相应单元格。
    【解决方案3】:

    如果异步下载是您真正的问题,请参阅AFNetworking 库的 UIImageView 类别。尤其是这个功能:

    - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholderImage
    

    您可以将此 UIimageview 类别包含到您的项目中,并设置从该类派生的单元格图像。

    【讨论】:

      【解决方案4】:

      在代理方法上,你必须在下载完成后更新图像,你可以使用

      [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] 
                       withRowAnimation:UITableViewRowAnimationAutomatic];
      

      这会再次调用

          -(CGFloat)tableView:(UITableView *)tableView
      heightForRowAtIndexPath:(NSIndexPath *)indexPath
      

      因此,您应该使用图像的高度(如果存在)、默认高度或占位符图像(如果有)。

      【讨论】:

        【解决方案5】:

        首先,如果您还没有这样做,我强烈建议您使用SDWebImage 进行异步下载。根据您的需要,您可以让它在内存或磁盘上缓存图像 - 后者以正确的方式完成,将它们保存在特殊的缓存文件夹中,因此 iOS 可以在空间不足时删除它们。它还在 UIImageView 上提供了一个方便的类别,类似于 AFNetworking 为您提供的,但具有更多选项和一个可选的完成块。这样你就可以做这样的事情:

        #import <SDWebImage/UIImageView+WebCache.h>
        
        ...
        
        - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        
            // Create the cell...
        
            [cell.myImageView setImageWithURL:[NSURL URLWithString:@"http://example.com/image.jpg"]
                      placeholderImage:[UIImage imageNamed:@"placeholder"]
                             completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
                if (image != nil) {
                    [self.images insertObject:image atIndex:indexPath.row];
                    [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:YES];
                }
            }];
            return cell;
        }
        

        这样你异步下载图像,正确设置 UIImageView,但你也使用块将图像保存在 NSMutableArray 中,所以你可以告诉 tableview 正确的高度。它还告诉表格视图更新已加载图像的单元格的高度。然后当表格视图需要知道给定单元格的高度时,如果图像已经加载,我们将从中获取大小:

        - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
            id image = [self.images objectAtIndex:indexPath.row];
            if ([image isKindOfClass:[NSNull class]]) {
                return defaultHeight;
            } else {
                return [image size].height + topPadding + bottomPadding;
            }
        }
        

        这样我们甚至可以知道屏幕外图像的高度(例如,当您滚动到底部时)。此解决方案假定您首先使用 NSNull 值填充 NSMutableArray,然后在加载时将其替换为图像。最后,如果您愿意,甚至可以在单元格显示之前使用 SDWebImageManager 手动开始下载。

        【讨论】:

        • 我同意 SDWebImage 是一个很棒的库,但它不应该是这个问题的正确答案,它应该得到肯定的投票,但不是赏金。
        【解决方案6】:

        您可以根据下载的图片设置UIImageView 的大小,但它会看起来很糟糕

        所以我建议你把你所有的图片都制作成相同的大小,这样看起来会很好看

        您可以使用它来制作相同大小的所有图像。

        + (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;
        }
        

        但是,如果您真的想显示具有不同行大小的表格,那么请在运行时更改您的行大小

        当您将获得图像时,将您的图像保存在字典中,键值为行的 indexPath。

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

        然后重新加载表格行。

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

        它会改变你的行高

        -(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;
            }
        }
        

        【讨论】: