我正在构建一个 UICollectionView
,我的自定义单元格将包含两个标签和一个图像。
每张图片都是异步下载的,所以在下载完成之前我不知道它的大小。 下载后,我想调整每个单元格以重新布局其内容和框架,以适应刚刚下载的图像的高度。
作为 UICollectionViewLayout
,我正在使用 CHTCollectionViewWaterfallLayout
要异步下载图像,我使用 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 ...}];
问题:
下载图像后立即调整每个 UICollectionViewCell
大小的正确方法是什么?
最佳答案
当图像返回时,您应该能够在动画 block 中使您的 Collection View 布局无效。如果不止一张图像同时完成,事情可能会变得有点复杂,但这应该可行:
[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];
}];
}];
然后在适当的委托(delegate)方法中返回一个不同的尺寸。
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return /* a different size if the image is done downloading yet */;
}
关于ios - 下载内部图像后调整 UICollectionView 单元格的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23121663/