title: Dynamically change cell size according to image in a UICollectionView
body: <p>I am displaying dynamic images recieved from server in a horizontal collection view. When I set up the collectionview I set this:</p>
  
  <pre><code>-(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];
  </code></pre>
  
  <p>}</p>
  
  <p>For cellForRowAtIndexPath I download the image like this: </p>
  
  <pre><code>-(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;
  </code></pre>
  
  <p>}</p>
  
  <p>However there are <strong>both portrait and landscape images</strong> displayed in the collection view. Portrait Images are displaying just fine but landscape images are appearing to small in the collectionview. <strong>What I need to do is resize the cell according to the image size so that they fill up correct aspect ratio</strong>? </p>
  
  <p>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: </p>
  
  <pre><code>    - (CGSize)collectionView:(UICollectionView *)collectionView
                layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
  </code></pre>
  
  <p>{</p>
  
  <pre><code>return CGSizeMake(165, 255);
  </code></pre>
  
  <p>}</p>
  
  <p>Hence, I am wondering how can I achieve this feature, Let me know if more information is needed in this regard?</p>