title: AVPlayerLayer in UICollectionViewCell, or how to load gifs as WhatsApp
body: <p>In the app I have a <code>UICollectionView</code> with item size so that about ~20 items are visible on the screen at the same time. The content that I want to display in each cell is Gif image downloaded from Giphy / Tenor. </p>
  
  <p>However, I realized that gif files take much more space (and time to load) than the relative mp4 files that both Tenor and Giphy provide for each animated image, which is actually obvious, cause mp4 file format has a compressing logic and stuff like that. Sorry if I use wrong terms.</p>
  
  <p>In order to have list loaded faster I decided to switch using <code>UIImageView</code> with GIF images to <code>AVPlayerLayer</code>, cause mp4 file is like ~10x lighter than GIF image. But I faced with the performance issue similar to what described <a href="https://stackoverflow.com/questions/40474480/how-many-avplayers-are-allowed-to-be-created-at-the-same-time">HERE</a>. The flow is mostly the same, I have 20+ items visible at the same time, but because of hardware limitations it only shows 16 videos. I couldn't find any workaround or any other frameworks that would allow to have more than 16 <code>AVPlayerLayer</code> showing video at the same time. </p>
  
  <p><strong>I'm wondering</strong> how <strong>WhatsApp</strong> application works and handles this logic. It also has GIF selection from Tenor. I already checked and figured out that WhatsApp downloads small video files and not gif images. That's why it loads very fast. But I have no idea how they can show 20+ items at the same time. HERE is how that works in WhatsApp - <a href="https://media.giphy.com/media/33E84h3RAVn0vQWZak/giphy.gif" rel="noreferrer">https://media.giphy.com/media/33E84h3RAVn0vQWZak/giphy.gif</a>. Also, I notices during scroll the small static previews are showing, but I don't see the app making requests for it. Probably they gets a first frame of the gif on the fly without any delays in main thread. </p>
  
  <p>I also tried that, but even if I make every single stuff in background thread and the only line on the main thread is "self.imageView.image = myImage", it anyway is lugging a little bit if I have 8 items in the row for example and scrolling very fast.</p>
  
  <p>I see only 2 possible solutions to have it loads fast (so we definitely need to load mp4 instead of gifs), and scroll smooth and without lugs:<br>
  1. WhatsApp uses its own custom Video Core to display video in the UICollectionViewCell .<br>
  2. WhatsApp downloads video to speed up the download process but then encodes mp4 file to gif one on the fly and use regular animated UIImageView to show the output gif file. However I was not able to have this flow working very fast without lugging during 'massive' scrolling</p>
  
  <p><strong>Any thoughts on how to implement the same to make it works fast and smooth as in WhatsApp?</strong> I'm unable to check how it handles the downloaded info, but for sure it downloads mp4 files and not gif ones.</p>