根据UICollectionView中的图像动态更改单元格大小 [英] Dynamically change cell size according to image in a UICollectionView

查看:240
本文介绍了根据UICollectionView中的图像动态更改单元格大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在水平集合视图中显示从服务器收到的动态图像。当我设置集合视图时,我设置了这个:

I am displaying dynamic images recieved from server in a horizontal collection view. When I set up the collectionview I set this:

-(void)setupCollectionView{
[self setupPageControlView];
self.screensCollectionView.delegate=self;
self.screensCollectionView.dataSource=self;
[self.screensCollectionView registerNib:[UINib nibWithNibName:@"FGAppScreensCollectionViewItemCell" bundle:nil] forCellWithReuseIdentifier:@"screenCell"];


UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
[flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
[flowLayout setMinimumInteritemSpacing:0.0f];
[flowLayout setMinimumLineSpacing:0.0f];
[flowLayout setItemSize:CGSizeMake(165, 255)];
//[self.screensCollectionView setPagingEnabled:YES];
[self.screensCollectionView setCollectionViewLayout:flowLayout];

}

对于cellForRowAtIndexPath,我下载了像这样的图像:

For cellForRowAtIndexPath I download the image like this:

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentifier = @"screenCell";

FGAppScreensCollectionViewItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];

NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:[self.imageURLArray objectAtIndex:indexPath.row]]];

UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[spinner startAnimating];
[spinner setFrame:CGRectMake(50, 100, 20, 20)];
[cell.contentView addSubview:spinner];
[cell.screenImageView setImageWithURLRequest:urlRequest placeholderImage:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
    //STOP THE SPINNER
    for (UIView* view in [cell.contentView subviews]) {
        if ([view isKindOfClass:[UIActivityIndicatorView class]]) {
            [view removeFromSuperview];
        }
    }
    cell.screenImageView.image = image;

} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
    //
}];
[cell.screenImageView setImageWithURL:[NSURL URLWithString:[self.imageURLArray objectAtIndex:indexPath.row]]];
if (self.delegate) {
      UITapGestureRecognizer* g = [[UITapGestureRecognizer alloc] initWithTarget:self.delegate action:@selector(showPopUpImageView:)];
      [cell.screenImageView addGestureRecognizer:g];
}

return cell;

}

然而有纵向和横向图像显示在集合视图中。肖像图像显示得很好,但在集合视图中景观图像看起来很小。 我需要做的是根据图片大小调整单元格大小,以便填充正确的宽高比

However there are both portrait and landscape images displayed in the collection view. Portrait Images are displaying just fine but landscape images are appearing to small in the collectionview. What I need to do is resize the cell according to the image size so that they fill up correct aspect ratio?

我添加了如下所示的方法,但问题是它在开始时一次调用所有单元格,并且一旦我在cellForRowAtIndexPath方法中下载图像就不会被调用:

I added method like this below but the problem is it gets called for all cells at once in the beginning and won't be called once I download the image in cellForRowAtIndexPath method:

    - (CGSize)collectionView:(UICollectionView *)collectionView
              layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath

{

return CGSizeMake(165, 255);

}

因此,我想知道如何实现此功能,如果需要更多信息,请告诉我?

Hence, I am wondering how can I achieve this feature, Let me know if more information is needed in this regard?

推荐答案

-setImageWithURLRequest:... ,当图像加载将其缓存在哈希表中,然后使用当前图像路径调用 -reloadItemsAtIndexPaths: ,重新加载单元格,从而导致CollectionView重新检查单元格的大小。

In -setImageWithURLRequest:..., when the image loads cache it in a hash table, and then call -reloadItemsAtIndexPaths: with the current image path, to reload the cell and thus causing the CollectionView to re-check what size the cell should be.

确保在查找图像时查阅哈希表。如果collectionView项目在屏幕外滚动并重新打开,这也可以节省额外的网络访问。

Make sure you consult the hash table when looking up the image. This will also save you extra network accesses in cases where the collectionView items scroll offscreen and back on again.

注意我担心如果你使用 UICollectionReusableView 子类您的代码可能会失败,因为单元格可以在其图像加载之前重新使用,因此图像将加载到错误的位置。

Note I am worried that if you’re using a UICollectionReusableView subclass your code can fail, because cells can be re-used before their images ever load, so the images will load in the wrong place.

这篇关于根据UICollectionView中的图像动态更改单元格大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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