【发布时间】:2017-03-30 12:55:20
【问题描述】:
我一直在尝试使用放置在 TableView 单元中的 AVPlayer 将一些视频添加到我的 tableView 中。
但在加载视频时 UI 会冻结。
这是我在 indexpath 中的 cellforRow。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCel {
var tableCell : UITableViewCell? = nil
if (tableCell == nil){
tableCell = tableView.dequeueReusableCell(withIdentifier: "cell")
}
let view = tableCell?.viewWithTag(9999)
tableCell?.tag = indexPath.row
DispatchQueue.main.async {
if (tableCell?.tag == indexPath.row){
let player = self.cache.object(forKey: indexPath.row as AnyObject) as! AVPlayer
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = (view?.bounds)!
view?.layer.addSublayer(playerLayer)
player.play()
}
// tableView.reloadData()
}
return tableCell!
}
这就是我将视频添加到缓存的方式。
for i in 0...10{
var videoURL : URL? = nil
if (i%2 == 0){
videoURL = URL(string: "https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")
}else{
videoURL = URL(string: "http://techslides.com/demos/sample-videos/small.mp4")
}
let player = AVPlayer(url: videoURL!)
arr?.append(player)
self.cache.setObject(player, forKey: i as AnyObject)
print("count = \(arr?.count)")
}
解决此问题的最佳方法是什么?
【问题讨论】:
-
你是怎么解决的?
-
@FaridAlHaddad 我进行了设计更改,这部分被取消了。在新设计中,我必须导航到新页面,然后从那里显示视频。
-
哦好的..谢谢!
标签: ios swift uitableview