繁体   English   中英

SwiftUI 如何在UIImagePickerController 中编辑视频?

[英]SwiftUI How to Edit video in UIImagePickerController?

我制作了一个视频记录 function。我想知道如何在按下“使用视频”按钮之前使用此栏编辑视频。 在此处输入图像描述 喜欢这个视频。 下面是我的代码。

struct ContentView: View {
    @State private var isShowCamara = false
    var body: some View {
            Button {
                isShowCamara.toggle()
            } label: {
                Text("Record and Save Video")
            }
        }
        .sheet(isPresented: $isShowCamara) {
            RecordVideoPicker(sourceType: .camera)
            
        }
    }
}
struct RecordVideoPicker: UIViewControllerRepresentable {
    @Environment(\.presentationMode) private var presentationMode
    var sourceType: UIImagePickerController.SourceType = .camera
    func updateUIViewController(_ uiViewController: UIViewController, context: Context) {
        
    }
    func makeUIViewController(context: UIViewControllerRepresentableContext<RecordVideoPicker>) -> UIViewController {
        let mediaUI = UIImagePickerController()
        mediaUI.sourceType = sourceType
        mediaUI.mediaTypes = [kUTTypeMovie as String]
        mediaUI.allowsEditing = true
        mediaUI.delegate = context.coordinator
        return mediaUI
    }
    final class Coordinator : NSObject, UIImagePickerControllerDelegate, UINavigationControllerDelegate{
        var parent: RecordVideoPicker

        init(_ parent: RecordVideoPicker) {
                self.parent = parent
        }
                func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
                    parent.presentationMode.wrappedValue.dismiss()
                    guard
                      let mediaType = info[UIImagePickerController.InfoKey.mediaType] as? String,
                      mediaType == (kUTTypeMovie as String),
                      let url = info[UIImagePickerController.InfoKey.mediaURL] as? URL,
                      UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(url.path)
                      else { return }
                    UISaveVideoAtPathToSavedPhotosAlbum(url.path, self, nil
                        ,nil)
                }
        
    }
    func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }
}

我希望流程是录制视频,它可以编辑视频长度(用户可以使用屏幕上方的栏进行编辑),然后按使用视频按钮保存视频。

要编辑视频,您应该使用UIVideoEditorController 您需要将它包装在UIViewControllerRepresentable中,就像您对UIImagePickerController所做的那样。

来自Apple 的文档

当您的目的是为电影编辑提供界面时,请使用视频编辑器 [ UIVideoEditorController ]。 虽然UIImagePickerController class 也允许用户修剪电影,但它的主要作用是选择保存的图片和电影,以及捕获新的图片和电影。

题解是我写了两个function,picker一个视频得到它的URL,另一个是根据URL剪辑视频保存。 我尝试编写一个结构(编辑视频),如下所示。 它可以编辑视频并保存,但是
我不知道为什么它保存 function 做两次。

struct VideoEditor: UIViewControllerRepresentable {
var videoURL: URL
@Environment(\.presentationMode) private var presentationMode
func makeUIViewController(context: UIViewControllerRepresentableContext<VideoEditor>) -> UIVideoEditorController {
    let editor = UIVideoEditorController()
    editor.videoPath = videoURL.path
    editor.delegate = context.coordinator
    return editor
}
func updateUIViewController(_ uiViewController: UIVideoEditorController, context: UIViewControllerRepresentableContext<VideoEditor>) {
}

func makeCoordinator() -> Coordinator {
    Coordinator(self)
}

final class Coordinator: NSObject, UIVideoEditorControllerDelegate,UINavigationControllerDelegate {
    var parent: VideoEditor

    init(_ parent: VideoEditor) {
        self.parent = parent
    }
    func videoEditorController(_ editor: UIVideoEditorController, didSaveEditedVideoToPath editedVideoPath: String) {
        // Save the edited video to the camera roll
        parent.presentationMode.wrappedValue.dismiss()
        UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(editedVideoPath)
        UISaveVideoAtPathToSavedPhotosAlbum(editedVideoPath, self, nil
            ,nil)
        
    }
    func videoEditorControllerDidCancel(_ editor: UIVideoEditorController) {
    }
}

}

问题未解决?试试以下方法:

SwiftUI 如何在UIImagePickerController 中编辑视频?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2023 STACKOOM.COM