我有一个包含全宽UIImageView
的单元格,我正在创建一个AVPlayerLayer
来播放内嵌视频。这几乎是完美的,唯一的问题是当它被添加到previewImage
图像视图时,它是一个完美显示的全宽图像视图,它在左侧和右侧有一个1像素的间隙。因此,当视频播放时,您可以真正注意到它并且它非常刺耳。
我这样设置:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: false)
let cell = tableView.cellForRow(at: indexPath) as! GameMediaCell
if (cell.playerLayer != nil) {
cell.playerLayer?.player?.pause()
cell.playerLayer?.removeFromSuperlayer()
cell.playerLayer = nil
} else {
if let clipURI = viewModel?.mediaURIForIndex(indexPath.row) {
let player = AVPlayer(url: clipURI)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = cell.previewImage.frame
cell.previewImage.layer.addSublayer(playerLayer)
cell.playerLayer = playerLayer
playerLayer.player?.play()
}
}
}
答案 0 :(得分:4)
在将播放器图层添加为子视图之前添加此行。
self.playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
答案 1 :(得分:0)
这对我有用:
//MARK: Add a new variable to your class where you are using AVPLayer
let screenRect = UIScreen.mainScreen().bounds
//MARK:Than just create a new frame of AVPLayerLayer
self.avPlayerLayer!.frame = CGRectMake(0, 0, self.screenRect.size.width, self.screenRect.size.height)