2017-05-08 54 views
0

我有一個包含全寬UIImageView的單元格,並且我創建了一個AVPlayerLayer以播放內聯視頻。這個工作幾乎完美,唯一的問題是,當它被添加到previewImage圖像視圖時,它是一個完美顯示的全寬圖像視圖,它在左側和右側有1 px的間隔。所以當視頻播放的時候,你可以真正注意到它,這很刺耳。將AVPlayerLayer添加爲子圖層並不完全填充父視圖

我設置它像這樣:

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

回答

4

增加玩家層子視圖之前加入這一行。

self.playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill

+0

我發現這個有一天某個地方,這是正確的答案。應該回答我自己的問題,所以我會接受這個。謝啦 :) – user1898712

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)