根据UICollectionView中的图像dynamic更改单元大小

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

-(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我下载像这样的图像:

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

}

但是,在收集视图中显示纵向和横向图像 。 纵向图像显示得很好,但横向图像在采集视图中显得很小。 我需要做的是根据图像大小调整单元格大小,以填充正确的高宽比

我在下面添加了这样的方法,但问题是一开始就一次调用所有单元格,一旦我在cellForRowAtIndexPath方法中下载图像,将不会被调用:

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

{

 return CGSizeMake(165, 255); 

}

因此,我想知道如何实现这个function,让我知道在这方面是否需要更多的信息?

-setImageWithURLRequest:... ,当图像加载caching在散列表中,然后调用-reloadItemsAtIndexPaths:与当前图像path重新加载单元格,从而导致CollectionView重新检查单元格的大小。

确保在查找图像时查阅散列表。 这样可以节省额外的networking访问权限,以防collectionView项目在屏幕上滚动并重新打开。

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

但是添加一个UICollectionView的委托方法。

 - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[arryOfImg objectAtIndex:indexPath.row]]]; //You may want to create a divider to scale the size by the way.. return CGSizeMake((image.frame.size.width)/4.0, (image.frame.size.height)/4.0); }