gpt4 book ai didi

ios - UICollectionView : scrolling issue 内的视频

转载 作者:行者123 更新时间:2023-12-01 20:10:04 26 4
gpt4 key购买 nike

我在 UICollectionView 中显示了一些视频;当应用程序加载时它运行良好,而当用户滚动 UICollectionView 时,视频没有加载并且所有单元格都变灰并且永远不会填充正确的视频,即使在调试中我看到了正确的 mp4 url​​。

这是代码:

    func collectionView(cv: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

let videoCell = collectionView!.dequeueReusableCellWithReuseIdentifier("VideoCell", forIndexPath: indexPath) as? UICollectionViewCell

var aPlayer = AVPlayer()
let moviePlayerController = AVPlayerViewController()

let item = self.wall[indexPath.row]

print("URL:"+item.video_link)

let fileUrl = NSURL(string: item.video_link)

dispatch_async(dispatch_get_main_queue(), {
aPlayer = AVPlayer(URL: fileUrl!)

moviePlayerController.player = aPlayer
moviePlayerController.view.frame = CGRectMake(0, 0, videoCell!.frame.size.width, videoCell!.frame.size.height)
moviePlayerController.videoGravity = AVLayerVideoGravityResizeAspectFill
moviePlayerController.view.sizeToFit()
moviePlayerController.player?.actionAtItemEnd=AVPlayerActionAtItemEnd.None
NSNotificationCenter.defaultCenter().addObserver(self,
selector: "playerItemDidReachEnd:",
name: AVPlayerItemDidPlayToEndTimeNotification,
object: moviePlayerController.player!.currentItem)
moviePlayerController.showsPlaybackControls = false
moviePlayerController.player?.play()
/*
videoCell?.layer.shouldRasterize = true;
videoCell!.layer.rasterizationScale = UIScreen.mainScreen().scale;
*/
videoCell!.addSubview(moviePlayerController.view)
})
return videoCell!
}

谁能帮我理解这个问题?

最佳答案

  • 您应该将 subview 添加到 videoCell!.contentView不是 videoCell本身。
  • 您正在添加大量 moviePlayerController.view s 到一个单元格中! cellForItemAtIndexPath每当要显示单元格时都会调用它,但这并不意味着在出列后您将拥有一个干净的单元格,它可能位于不再显示在屏幕上的先前单元格中。有一个Bool或查看 videoCell!.subviews.count确定您之前是否添加了 moviePlayerController.view或不。如果您已经添加了它,请更改 url(如果可能)或删除它,然后重新添加。

  • ps:最好的方法是定义一个自定义 UICollectionViewCell类(class)。

    关于ios - UICollectionView : scrolling issue 内的视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37905478/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com