Когда я использую AVPlayer внутри UICollectionView, сталкиваюсь с таким количеством проблем с производительностью, как:
Сбой приложения из-за большого использования памяти. Чтобы решить эту проблему, я позвонил
override func prepareForReuse()
{
super.prepareForReuse()
self.player = nil
self.playerLayer.removeFromSuperlayer()
}
Когда я делаю это и во время быстрой прокрутки, CollectionCell не загружает предыдущие элементы. Он показывает белый экран и затем загружает контент.
Вот так выглядит моя ячейка Custom CollectionView:
var model: RecordedVideoModel! {
didSet {
//Customize
let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.light)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.frame = (self.blurView?.bounds)!
blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
blurView.addSubview(blurEffectView)
//VideoLabel and Text
self.videoLabel?.textColor = UIColor.white
self.videoLabel?.font = UIFont.init(name: FontTypeBold, size: lightTextSize)
self.videoLabel?.text = model.username
//Duration/TimeStamp and Text
self.durationLabel?.textColor = UIColor.white
self.durationLabel?.font = UIFont.init(name: FontTypeRegular, size: lightTextSize)
let now = Date()
self.durationLabel.text = String(format:"%@ ago",now.offset(from: DateConverter.getDateFromString(dateToConvert: model.session_timestamp!)))
let contentUrl = URL(string: String(format:"%@",model.video_cdn_url!))
player = AVPlayer(url: contentUrl!)
playerLayer = AVPlayerLayer.init(player: player)
playerLayer.frame = (self.videoView?.bounds)!
self.playerLayer?.backgroundColor = UIColor.clear.cgColor
playerLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
self.videoView?.layer.addSublayer(playerLayer)
player?.isMuted = true
player!.addPeriodicTimeObserver(forInterval: CMTimeMakeWithSeconds(1, 1), queue: DispatchQueue.main) { (progressTime) -> Void in
let duration = CMTimeGetSeconds(progressTime)
let seconds = Int(duration .truncatingRemainder(dividingBy: 60))
if seconds == 5 {
self.player?.seek(to: kCMTimeZero)
}
}
//player?.play()
player?.playImmediately(atRate: 1.0)
}
}
override func prepareForReuse()
{
super.prepareForReuse()
//self.player = nil
self.playerLayer.removeFromSuperlayer()
self.player?.pause()
for view in self.blurView.subviews
{
view.removeFromSuperview()
}
}
Мне нужна плавная прокрутка CollectionView с меньшим использованием памяти. Кто-нибудь может предложить решение для этого?