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?