16.6 C
London
Sunday, September 15, 2024

ios – Selecting A Video File From PhotoLibrary With SwiftUI


I’m utilizing following code to get video’s URL that are in picture library.

It presents record of video information in picture library and also you select one among them.

It then opens an different view with cancel, play and select buttons at backside.

You must contact select button right here once more to get URL of the video file.

I wish to get URL at first step however how.

struct VideoPickerView: UIViewControllerRepresentable {

var videoURL: URL?

@Setting(.presentationMode) personal var presentationMode

class Coordinator: NSObject, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
    var mother or father: VideoPickerView

    init(mother or father: VideoPickerView) {
        self.mother or father = mother or father
    }

    
    
    
    func imagePickerController (
        _ picker: UIImagePickerController,
        didFinishPickingMediaWithInfo data: [UIImagePickerController.InfoKey: Any]
    ) {
        
        
        
        if let mediaType = data[.mediaType] as? String,
              mediaType == UTType.film.identifier,
              let videoURL = data[.mediaURL] as? URL ?? data[.mediaURL] as? URL {

              // Deal with the video URL
              print("url (videoURL)")

              @ObservedObject var fileInfo = SharedFileInfo.shared
              mother or father.videoURL = videoURL
              fileInfo.videoURL = videoURL
              fileInfo.menuIndex = 34
          }

        mother or father.presentationMode.wrappedValue.dismiss()
    }

    // high blue cancel
    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        @ObservedObject var fileInfo = SharedFileInfo.shared
       
        fileInfo.menuIndex = 0
        mother or father.presentationMode.wrappedValue.dismiss()
        
    }
}

func makeCoordinator() -> Coordinator {
    return Coordinator(mother or father: self)
}

func makeUIViewController(context: Context) -> UIImagePickerController {
    let picker = UIImagePickerController()
    picker.delegate = context.coordinator
    picker.sourceType = .photoLibrary
    picker.mediaTypes = [UTType.movie.identifier]
    picker.allowsEditing = false


    return picker
}

func updateUIViewController(_ uiViewController: UIImagePickerController, context: Context) {}

}

Latest news
Related news

LEAVE A REPLY

Please enter your comment!
Please enter your name here