Search code examples
videoswiftuicameraavfoundation

SwiftUI How to Edit video in UIImagePickerController?


I made a Video record function. and i want to know how to use this bar the edit video before i press Use Video button .enter image description here like this video . and below is my code.

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

I wish the flow is record a video and it can edited the video length(user can use the bar above the screen to edit),and press Use Video button to save the video.


Solution

  • To edit videos, you should use UIVideoEditorController. You'll need to wrap it in UIViewControllerRepresentable as you did for UIImagePickerController.

    From Apple's documentation:

    Use a video editor [UIVideoEditorController] when your intent is to provide an interface for movie editing. While the UIImagePickerController class also lets a user trim movies, its primary roles are choosing saved pictures and movies, and capturing new pictures and movies.