Resize UICollectionView cells after image inside has been downloaded

15,877

Solution 1

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 */;
}

Solution 2

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.

Share:
15,877
Fmessina
Author by

Fmessina

Updated on June 15, 2022

Comments

  • Fmessina
    Fmessina about 2 years

    I'm building a UICollectionView and my custom cells will contain two labels and one image.

    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

    To download the image asynchronously I'm using SDWebImage, like this:

        [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?

  • Fmessina
    Fmessina about 10 years
    When should I reload the collection view, after all the images are downloaded? Or it's better to reload each cell with reloadItemsAtIndexPaths?
  • Fmessina
    Fmessina about 10 years
    Thanks! I'm getting a perfect result the first time the UICollectionView is loaded, but when it's loaded for the second time (i.e. moving to another view and coming back) I get weird cell frame sizes (smaller than expected, with content overlapping). If I scroll a bit, everything is displayed properly again. Do I have to change the cell frame size (and where) after the image is downloaded?
  • Fmessina
    Fmessina about 10 years
    Solved. Actually there was nothing do nothing with the cell frame. I just had to set the cell.imageView frame just before the animateWithDuration block. [cell.imageView setFrame:CGRectMake(2,2,image.size.width,image.size.height])‌​]; [UIView animateWithDuration:0.3f animations:^{ [self.collectionView.collectionViewLayout invalidateLayout]; }];
  • Chris
    Chris about 10 years
    Doing it in this way you would have to reload the collection view after downloading all of the images.
  • Fmessina
    Fmessina about 10 years
    +1 because even if your answer didn't solve my issue, I used the idea of a [cell size] method. However @Ash solution was what I was looking for.
  • Lion789
    Lion789 about 10 years
    Should I be calling it this way with a callback because if I do I have an issue since it expects a return expression, since I have it called on each cell? stackoverflow.com/questions/23316634/… That is how I wrote it, and was actually trying the resize but it does not keep the aspect ratio and instead cuts out a piece of the image?
  • Matt
    Matt over 9 years
    You should not call dequeueReusableCellWithReuseIdentifier: multiple times without returning the cell to the collection, e.g. in cellForItemAtIndexPath:. The collection will have hundreds of dequeued cells sitting around that are never returned to be reused.
  • Chris
    Chris over 9 years
    @Matt, thanks for pointing that out! I updated the answer to use cellForItemAtIndexPath:.
  • Waqar Khalid
    Waqar Khalid almost 6 years
    What did you return in size sizeForItemAtIndexPath method?