Mohamed
Mohamed

Reputation: 59

Picking a video using UIImagePickerController in SwiftUI

I have started developing on SwiftUI recently and right now I am facing difficulties trying to show the video that I pick from the UIImagePicker by storing the Data in imageData in the function imagePickerController

Here's my code:

struct ImagePicker: UIViewControllerRepresentable {

      @Binding var pickedImage: Image
      @Binding var imageData: Data
      @Binding var isShown: Bool
      @Binding var url: URL?

      class Coordinator: NSObject, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
        
        
          @Binding var isShown: Bool
          @Binding var url: URL?

         // I have tried using a new variable (parentImagePicker) here and I added it to the initializer so that I can store the imageData in it in the function (imagePickerController) below but I wasn't able to implement it correctly   
        
          init( isShown: Binding<Bool>, url: Binding<URL?>) {
              _isShown = isShown
              _url = url
              
          }
        

          func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {

              
  
                          guard let mediaType = info[.mediaType] as? String,
                              mediaType == (kUTTypeMovie as String),
                              let uiURL = info[.mediaURL] as? URL
                              else { return }
            
            
            
                          url = uiURL
                          isShown = false
                      }

          func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
              isShown = false
          }

      }

      func makeCoordinator() -> Coordinator {
        return Coordinator( isShown: $isShown, url: $url)
      }

      func makeUIViewController(context: UIViewControllerRepresentableContext<ImagePicker>) -> UIImagePickerController {
          let picker = UIImagePickerController()
          picker.mediaTypes = [kUTTypeMovie as String]
          picker.delegate = context.coordinator
          return picker
      }

      func updateUIViewController(_ uiViewController: UIImagePickerController,
                                  context: UIViewControllerRepresentableContext<ImagePicker>) {
      }
  }

  fileprivate func convertFromUIImagePickerControllerInfoKeyDictionary(_ input: [UIImagePickerController.InfoKey: Any]) -> [String: Any] {
      return Dictionary(uniqueKeysWithValues: input.map {key, value in (key.rawValue, value)})
  }

  fileprivate func convertFromUIImagePickerControllerInfoKey(_ input: UIImagePickerController.InfoKey) -> String {
      return input.rawValue
  }

Upvotes: 0

Views: 2252

Answers (1)

matt
matt

Reputation: 535889

You shouldn't ever hold the data from a video in memory. It's way too big! You've got the URL of the video file on disk. That's all you need. The URL may be temporary, so immediately tell the file manager to copy the file to some safe location, such as the Documents folder. (Copy, not move; you might not have permission to move it.) Now when you want to play the video, use the new URL of the video file in the Documents folder and you'll be fine.

Upvotes: 2

Related Questions