简体   繁体   English

下载图像内部后调整UICollectionView单元格的大小

[英]Resize UICollectionView cells after image inside has been downloaded

I'm building a UICollectionView and my custom cells will contain two labels and one image. 我正在构建一个UICollectionView ,我的自定义单元格将包含两个标签和一个图像。

Each image is downloaded asynchronously so I don't know it's size until the download it's complete. 每个图像都是异步下载的,因此在下载完成之前我不知道它的大小。 Once is downloaded, I want to adapt each cell to re-layout it's content and frame to fit in height the image just downloaded. 下载后,我想调整每个单元格以重新布局它的内容和框架,以适应刚刚下载的图像的高度。

As UICollectionViewLayout , I'm using CHTCollectionViewWaterfallLayout 作为UICollectionViewLayout ,我正在使用CHTCollectionViewWaterfallLayout

To download the image asynchronously I'm using SDWebImage , like this: 要异步下载图像我正在使用SDWebImage ,如下所示:

    [cell.imageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"]
                   placeholderImage:[UIImage imageNamed:@"placeholder.png"]
                          completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) 
                                {... some completion code here ...}];

QUESTION: 题:

What is the right approach to resize each UICollectionViewCell right after the image is downloaded? 在下载图像后立即调整每个UICollectionViewCell的正确方法是什么?

You should just be able to invalidate your collection view layout in an animation block when the image comes back. 您应该只能在图像返回时使动画块中的集合视图布局无效。 Things might get a little complicated if more than one image finishes completion at once, but this should work: 如果一次完成多个图像完成,事情可能会变得有点复杂,但这应该有效:

[cell.imageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"]
               placeholderImage:[UIImage imageNamed:@"placeholder.png"]
                      completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType)^{
                          [UIView animateWithDuration:0.3f animations:^{
                              [self.collectionView.collectionViewLayout invalidateLayout];
                          }];
                      }];

Then just return a different size in the appropriate delegate method. 然后在适当的委托方法中返回不同的大小。

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return /* a different size if the image is done downloading yet */;
}

To resize collection view cells, you could reload the collection view and return the size of you collection view cells dynamically in the method: 要调整集合视图单元格的大小,您可以重新加载集合视图并在方法中动态返回集合视图单元格的大小:

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
    return [cell size];
}

In cases when the cell's size is dynamic, get the cell from the index path and return its size based on the size of the image. 如果单元格的大小是动态的,则从索引路径获取单元格并根据图像的大小返回其大小。 I usually create a method for the cell to return a dynamic size, like in the example above. 我通常为单元格创建一个返回动态大小的方法,如上例所示。 You can use UIImage's size property to help return the size based on the image. 您可以使用UIImage的size属性来帮助返回基于图像的大小。

您可以尝试reloadItemsAtIndexPaths:对于已完成加载的单元格。

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

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