2

I have a UICollectionView which displays photos and videos. I was using an AVPlayer to display videos, however this resulted in very choppy scrolling. To combat this I am using the VideoNode from the AsyncDisplayKit. Currently in my cellForItemAt method I do the following:

    cell.viewWithTag(300)?.removeFromSuperview()

    if (image) {
        //show image
    }
    else if (video) {
                let mainNode = ASDisplayNode()
                let videoNode = ASVideoNode()

                DispatchQueue.background {

                    videoNode.frame = CGRect(x: 0.0,y:0.0,width: cell.bounds.width,height: cell.bounds.height)
                    videoNode.gravity = AVLayerVideoGravityResizeAspectFill
                    videoNode.shouldAutoplay = true
                    videoNode.shouldAutorepeat = true
                    videoNode.muted = true
                    videoNode.asset = AVAsset(url: cached_url)

                }

                DispatchQueue.main.async {
                    mainNode.addSubnode(videoNode)
                    mainNode.view.tag = 300
                    cell.addSubview(mainNode.view)
                    cell.sendSubview(toBack: mainNode.view)
                    videoNode.play()
                    cell.backgroundImageView.alpha = 0
                    cell.gradientOverlay.alpha = 1
                }
    }

However, with fast scrolling this is still a bit choppy, and the cells containing videos are briefly white before the video shows. Is there a way I could improve this code to further improve scrolling performance and make it as smooth as possible?

Alk
  • 5,215
  • 8
  • 47
  • 116
  • why do you use UICollectionView instead of ASCollectionNode? – Lukas Mar 15 '17 at 08:33
  • Whatever you use here to play video, at some point you will be loading multiple AVPlayer objects which will use alot of memory and resources and can also display so many at a time. You would be best to use a thumbnail and play button on the cell and present a video player on tap – Scriptable Jul 06 '17 at 15:28
  • @Alk Did you find a solution? –  Jan 24 '18 at 12:27
  • Nope, ended up using thumbnails in the CollectionView and playing videos in separate ViewControllers as the scrolling issues were too severe. – Alk Jan 24 '18 at 16:31

0 Answers0