将 AVPlayerLayer 添加为子层不会完全填充父视图

Adding AVPlayerLayer as a sublayer doesn't fill parent view fully

我有一个包含全宽度 UIImageView 的单元格,我正在创建一个 AVPlayerLayer 来播放内联视频。这几乎完美地工作,唯一的问题是当它被添加到 previewImage 图像视图时,这是一个完美显示的全宽图像视图,它在左侧和右侧有 1 px 的间隙。所以当视频播放的时候,你真的可以注意到它,而且非常刺耳。

我是这样设置的:

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()
        }
    }
}

在将播放器层添加为子视图之前添加此行。

self.playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill

这对我有用:

//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)