下载图片时,UITableViewCell的高度会调整大小 [英] UITableViewCell height resize when image is downloaded

查看:147
本文介绍了下载图片时,UITableViewCell的高度会调整大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 UIImageView + AFNetworking 类别进行异步图像加载。一切都可以正常工作,但是我已经尝试了几件事情,并且根据下载的图像调整单元格的高度大小并不成功。我想要的图像适合单元格的宽度,但调整高度,但我不知道如何完成。我已经尝试重新加载下载图像的行,但这只是导致 cellForRowAtIndexPath 再次启动并重新设置所有内容等等几乎递归。



我正在计算新的图像大小差异,并将其存储在 NSMutableArray 中,然后在UIImageView的 setImageWithURLRequest:placeholderImage:success:



我在中获得几行的正确高度heightForRowAtIndexPath 但是,表开始表现很奇怪,一切都覆盖着,等等。



任何想法?



谢谢!



编辑



我最终使用了以西结回答。我还写了一篇关于该主题的文章,并使用了这种技术的示例应用程序。



查看

您需要将下载的图像存储在内存或磁盘上,因此下次尝试从此URL中获取图像时,您将从缓存中收到。



所以如果你这样做比你必须这样做:

  [tableView beginUpdates]; 

[tableView reloadRowsAtIndexPaths:@ [indexPath] withRowAnimation:UITableViewRowAnimationFade];

[tableView endUpdates];

您应该在此表格视图数据源的方法中返回新单元格的高度:

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

我建议您使用 SDWebImage库而不是 AFNetworking ,因为它可以将您的图像缓存到memcache和磁盘,这很容易使用。所以如果你决定使用它,下载图像的代码将如下所示:

   - (UITableViewCell *)tableView: (UITableView *)tableView cellForRowAtIndexPath :( NSIndexPath *)indexPath {

...

[cell.imageView setImageWithURL:[NSURL URLWithString:@http:// www。
placeholderImage:[UIImage imageNamed:@placeholder.png]
success:^(UIImage * image,BOOL cached){

//将图像的高度保存到某个缓存
[self.heightsCache setObject:[NSNumber numberWithFloat:imHeight]
forKey:urlKey];

[tableView beginUpdates];
[tableView reloadRowsAtIndexPaths:@ [indexPath]
withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];
}
失败:^(NSError *错误){...此处失败代码...}];

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath :( NSIndexPath *)indexPath {
//尝试从自己的图像中获取图像高度高速缓存
//如果它不存在则返回默认值
CGFloat height = [[self.heightsCache objectForKey:urlKeyFromModelsArrayForThisCell] floatValue];
...
return newHeight;
}


I'm using UIImageView+AFNetworking category for async image loading. Everything works fine, but I've tried a couple of things and wasn't successful when resizing the cell's height according to the downloaded image. I want the image to fit to the width of the cell but resize in height, but I don't know how to accomplish that. I've tried reloading the row where the image was downloaded but that just causes the cellForRowAtIndexPath to fire again and set everything again, etc. Almost a recursion.

I'm calculating the new image size difference and storing that in NSMutableArray and then reloading the row in the success block in UIImageView's setImageWithURLRequest:placeholderImage:success:.

I get the correct height for a couple of rows in heightForRowAtIndexPath but then, the table starts acting weird and everything is overlaying, etc.

Any ideas?

Thanks!

EDIT

I eventually used Ezeki's answer. I also wrote a post on the topic and made a sample app that uses this technique.

Check it out on here.

解决方案

You need to store downloaded image in the memory or on the disk, so next time when you will try to get image from this URL you will received from cache.

So if you do that than you will have to do something like this:

[tableView beginUpdates];

[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];

[tableView endUpdates];

And you should return new cell's height in this method of table view data source:

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

I would recomend you to use SDWebImage library instead of AFNetworking because it can cache your images to memcache and disk for you and it is very easy to use. So if you decide to use it, your code of download images will be looking like this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    ...

    [cell.imageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"]
           placeholderImage:[UIImage imageNamed:@"placeholder.png"]
                    success:^(UIImage *image, BOOL cached) {

                        // save height of an image to some cache
                        [self.heightsCache setObject:[NSNumber numberWithFloat:imHeight] 
                                              forKey:urlKey];

                        [tableView beginUpdates];
                        [tableView reloadRowsAtIndexPaths:@[indexPath]
                                         withRowAnimation:UITableViewRowAnimationFade];
                        [tableView endUpdates];
                    }
                    failure:^(NSError *error) {... failure code here ...}];

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    // try to get image height from your own heights cache
    // if its is not there return default one
    CGFloat height = [[self.heightsCache objectForKey:urlKeyFromModelsArrayForThisCell] floatValue];
    ...
    return newHeight;
}

这篇关于下载图片时,UITableViewCell的高度会调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆