Determining which files are selected from the album in the imagePickerController delivery.

Asked 1 years ago, Updated 1 years ago, 88 views

I'm a beginner,
when I select a file from the album on my device. How do I describe the file when I determine whether it is an image or a video within the imagePickerControlle delivery?
We assume the following sources:

//Assume to be called from your smartphone album (library)
 funcimagePickerController(_picker:UIImagePickerController, didFinishPickingMediaWithInfo:[UIImagePickerController.InfoKey:Any]){

   if("image file"){
    Process A
   }
   else if ("Video File") {
    Processing B
   }

}

Thank you for your cooperation.

swift delegate uiimagepickercontroller

2022-09-30 19:46

1 Answers

Have you noticed the parameter info?In addition to the selected images and videos themselves, it also contains a lot of information.

The Key type UIImagePickerController.InfoKey gives you an idea of what information is (possibly) in it.

UIImagePickerController.InfoKey

Among them, mediaType may be used for your purpose.

mediaType

If you follow the documentation and write the code, it will look like this.

 funcimagePickerController(_picker:UIImagePickerController, DidFinishPickingMediaWithInfo:[UIImagePickerController.InfoKey:Any]){
        iflet mediaType = info [.mediaType] as ? US>String{
            if mediaType==kUTTypeImage as String{
                print ("Image")
                // Process A
                //...
            } else if mediaType==kUTTypeMovie as String{
                print ("Video")
                // Processing B
                //...
            } else{
                print(mediaType)
            }
        } else{
            print("Media type unspecified")
        }
    }

By the way, if you do not declare import MobileCoreServices, kUTTypeImage and kUTTypeMovie will fail.


2022-09-30 19:46

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.