使用prepareForReuse()方法在UICollectionViewCell被重复使用时对VideoCanvas进行重置。
示例代码:
class VideoCollectionViewCell: UICollectionViewCell {
var agoraKit: AgoraRtcEngineKit!
var videoCanvas: AgoraRtcVideoCanvas!
override func prepareForReuse() {
super.prepareForReuse()
videoCanvas.view?.removeFromSuperview()
agoraKit.setupLocalVideo(nil)
agoraKit.setupRemoteVideo(nil, uid: 0)
agoraKit.leaveChannel(nil)
agoraKit = nil
videoCanvas = nil
}
func setupAgoraKit() {
agoraKit = AgoraRtcEngineKit.sharedEngine(withAppId: "YOUR_APP_ID", delegate: nil)
agoraKit.enableVideo()
agoraKit.setChannelProfile(.liveBroadcasting)
}
func setupVideoCanvas() {
videoCanvas = AgoraRtcVideoCanvas()
videoCanvas.uid = 0
videoCanvas.renderMode = .hidden
videoCanvas.view = // Add your video view here
}
func joinChannel() {
agoraKit.joinChannel(byToken: "YOUR_TOKEN", channelId: "YOUR_CHANNEL_ID", info: nil, uid: 0, joinSuccess: nil)
}
func setupRemoteVideo() {
agoraKit.setupRemoteVideo(videoCanvas)
}
}