我有一个UICollectionView
显示照片和视频的设备。我正在使用一个AVPlayer
来显示视频,但这导致滚动非常不稳定。为了解决这个问题,我正在使用VideoNode
来自AsyncDisplayKit
. 目前在我的cellForItemAt
方法中,我执行以下操作:
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
}
}
然而,随着快速滚动,这仍然有点不稳定,并且包含视频的单元格在视频显示之前会短暂显示为白色。有没有办法改进此代码以进一步提高滚动性能并使其尽可能平滑?