如何呈现UIVideoEditorController

时间:2017-07-23 00:07:02

标签: ios swift

我从未使用过UIVideoEditorController,因此我不知道从哪里开始。

我希望当用户在我的集合视图单元格中选择视频时,会弹出视图控制器。

我已经知道视频的网址,所以我只需要有人向我展示如何正确呈现视图控制器。

这是我目前的代码

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let video = videosArray[indexPath.row]

视频变量是我想让他们编辑的视频

1 个答案:

答案 0 :(得分:9)

试试这个:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let video = videosArray[indexPath.row] // videosArray is a list of URL instances
    if UIVideoEditorController.canEditVideo(atPath: video.path) {
        let editController = UIVideoEditorController()
        editController.videoPath = video.path
        editController.delegate = self
        present(editController, animated:true)
    }
}

此外,您还需要添加UIVideoEditorControllerDelegate方法来解除显示的视频编辑器控制器:

extension YourViewController: UIVideoEditorControllerDelegate {
    func videoEditorController(_ editor: UIVideoEditorController, 
       didSaveEditedVideoToPath editedVideoPath: String) {
       dismiss(animated:true)
    }

    func videoEditorControllerDidCancel(_ editor: UIVideoEditorController) {
       dismiss(animated:true)
    }

    func videoEditorController(_ editor: UIVideoEditorController, 
               didFailWithError error: Error) {
       print("an error occurred: \(error.localizedDescription)")
       dismiss(animated:true)
    }
}