0

I have a UICollectionView with cells containing WKWebViews. When I scroll through these cells there is a lag because the WKWebViews aren't cached. Is there some way to cache the contents of the WKWebViews to prevent this lag?

For reference I am using the YouTubePlayer-Swift Cocoapod and this is my custom UICollectionViewCell:

import UIKit
import YouTubePlayer_Swift

class YouTubeVideoCollectionViewCell: UICollectionViewCell {
    
    let borderView = UIView(frame: .zero)
    var videoView = YouTubePlayerView(frame: .zero)
    
    var video: YouTubeVideo? {
        didSet {
            UISetUp()
        }
    }
    
    override open func preferredLayoutAttributesFitting(_ layoutAttributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutAttributes {
        self.updateConstraintsIfNeeded()
        return super.preferredLayoutAttributesFitting(layoutAttributes)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        changeToShadowedCard()
        self.contentView.layoutIfNeeded()
    }
    
    func changeToShadowedCard() {
        backgroundColor = .clear
        contentView.addSubview(borderView)
        borderView.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            borderView.topAnchor.constraint(equalTo: self.contentView.topAnchor),
            borderView.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor),
            borderView.rightAnchor.constraint(equalTo: self.contentView.rightAnchor),
            borderView.leftAnchor.constraint(equalTo: self.contentView.leftAnchor),
        ])
        borderView.layer.masksToBounds = true
        borderView.backgroundColor = UIColor.secondarySystemBackground
        borderView.layer.cornerRadius = 5
        borderView.addShadow(shadowColor: UIColor.label.cgColor, shadowOffset: CGSize(width: 0, height: 0), shadowOpacity: 0.6, shadowRadius: 3)
    }
    
    func UISetUp() {
        guard let id = video?.id else { return }
        borderView.addSubview(videoView)
        videoView.loadVideoID(id)
        videoView.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            videoView.topAnchor.constraint(equalTo: borderView.topAnchor),
            videoView.bottomAnchor.constraint(equalTo: borderView.bottomAnchor),
            videoView.rightAnchor.constraint(equalTo: borderView.rightAnchor),
            videoView.leftAnchor.constraint(equalTo: borderView.leftAnchor),
        ])
        videoView.clipsToBounds = true
        videoView.layer.cornerRadius = 5
    }
}

1 Answers1

0

You are asking two different questions here. The first one, about cache control of WKWebView - this question was asked and answered before I suggest you look for more info online. - here is one answer - Cache for WKWebView

About the package you are using to play youtube videos. First - it is using UIWebView and not WKWebView. Second im not sure about how Youtube caching works you should check it out. Because I don’t think you have caching for this streaming service

CloudBalancing
  • 1,461
  • 2
  • 11
  • 22