简体   繁体   English

根据下载的图像高度调整表格单元格的高度

[英]Resize the Table cell height as per downloaded image height

I am getting the image's URL at run time and I want to download & display these images in a table.我在运行时得到图像的 URL,我想下载并在表格中显示这些图像。 Images are going to be download asynchronously.图像将被异步下载。 What is more important is that I want to display all these images with their actual sizes.更重要的是,我想以实际尺寸显示所有这些图像。

Kindly help me.请帮助我。 Thanks in advance:)提前致谢:)

On the delegate method you have to update the image when finished downloading you can use在委托方法上,您必须在完成下载后更新图像,您可以使用

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

This will call again这将再次调用

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

So you should have it using the image's height if it exists, a default heigh, or a placeholder image if you have one.因此,您应该使用图像的高度(如果存在)、默认高度或占位符图像(如果有)。

First off, I'd highly recommend using SDWebImage for the asynchronous downloading, if you're not already doing so.首先,我强烈建议您使用SDWebImage进行异步下载,如果您还没有这样做的话。 Depending on what you want, you can have it cache images in memory or on disk - the latter is done in the correct fashion, by saving them in the special cache folder, so iOS can delete them if it runs out of space.根据您的需要,您可以让它在 memory 或磁盘上缓存图像 - 后者以正确的方式完成,将它们保存在特殊的缓存文件夹中,因此 iOS 可以在空间不足时删除它们。 It also provides a handy category on UIImageView, similar to what AFNetworking gives you, but with more options and and an optional completion block.它还在 UIImageView 上提供了一个方便的类别,类似于 AFNetworking 为您提供的类别,但具有更多选项和一个可选的完成块。 That way you can do something like this:这样你就可以做这样的事情:

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

That way you download the image asynchronously, set the UIImageView correctly, but you also use the block to save the image in an NSMutableArray, so you can tell the tableview the correct height.这样你就可以异步下载图像,正确设置 UIImageView,但你也可以使用块将图像保存在 NSMutableArray 中,这样你就可以告诉 tableview 正确的高度。 It also tells the table view to update the height of the cell whose image has loaded.它还告诉表视图更新其图像已加载的单元格的高度。 Then when the table view needs to know how high a given cell is, if the image has been loaded, we'll get the size from it:然后当表格视图需要知道给定单元格的高度时,如果图像已加载,我们将从中获取尺寸:

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

That way we know the height even of images that are off-screen (eg when you have scrolled to the bottom).这样我们甚至可以知道屏幕外图像的高度(例如,当您滚动到底部时)。 This solution assumes that you start by populating the NSMutableArray with NSNull values, which are then replaced with the images when they load.此解决方案假定您首先使用 NSNull 值填充 NSMutableArray,然后在加载图像时将其替换为图像。 Finally, if you like, you could use SDWebImageManager to start the downloads manually even before the cells are shown.最后,如果您愿意,甚至可以在显示单元格之前使用 SDWebImageManager 手动开始下载。

You can make your UIImageView size as per downloading image but it will give bad look .您可以根据下载的图像制作您的UIImageView大小,但它看起来会很糟糕

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.当您获得图像时,然后将图像保存在字典中,键值为行的indexPath。

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

Check out heightForRowAtIndexPath: .查看heightForRowAtIndexPath: Here's another post about it.这是关于它的另一篇文章

for that u have to create custom UITableviewCell and u can set the size of Table view cell using this code为此你必须创建自定义 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;
}

now Your Table cell's height is increase according to image height现在你的表格单元格的高度根据图像高度增加

If asynchronous downloading is your real issue, refer to AFNetworking library's UIImageView category.如果异步下载是您真正的问题,请参阅AFNetworking库的 UIImageView 类别。 Especially this function:特别是这个 function:

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

You can include this UIimageview category into your project and set cell images derived from that class.您可以将此 UIimageview 类别包含到您的项目中,并设置从该 class 派生的单元格图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM