DEV Community

Filip Němeček
Filip Němeček

Posted on

iOS: How to let user select video from their library

Despite its name UIImagePickerController can be also used to let user select video to import it to your app. It requires just a bit of configuration of the picker.

I am going to split the creation and configuration into multiple parts for clarity.

First we create the instance and assign self as a delegate which will let us dismiss the picker once selection is done and access the video.

let picker = UIImagePickerController()
picker.delegate = self

The delegate requires conformance to UIImagePickerControllerDelegate and UINavigationControllerDelegate. We will implement those methods later.

Media types configuration

Next we need to tell the picker what media types are we interested in and where it should look for them:

picker.sourceType = .photoLibrary
picker.mediaTypes = UIImagePickerController.availableMediaTypes(for: .photoLibrary) ?? []
picker.mediaTypes = ["public.movie"]

There is ?? [] fallback because the UIImagePickerController.availableMediaTypes method returns optional string array. You may consider adding assert below to check we got mediaTypes just to avoid surprises in the future.

Resolution configuration

And now we specify the quality and resolution because the picker does video compression:

picker.videoQuality = .typeHigh
picker.videoExportPreset = AVAssetExportPresetHEVC1920x1080

If you specify lower videoQuality then during the compression it will take that into an account. The videoExportPreset is super important because otherwise we would get 720p video.

The available presets are listed in the documentation.

For 4K we would use AVAssetExportPresetHEVC3840x2160 or possibly AVAssetExportPresetHEVCHighestQuality.

And that is basic configuration done.

It may be a good idea to allow editing with this parameter:

picker.allowsEditing = true

Now we just need to present the picker:

present(picker, animated: true, completion: nil)

The delegate methods

Let's start with the easier method:

func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        dismiss(animated: true, completion: nil)
}

This is called when user cancels the selection, so we dismiss the picker.

When user selects video, we get this callback:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        dismiss(animated: true, completion: nil)
        guard let movieUrl = info[.mediaURL] as? URL else { return }

        // work with the video URL
}

Once again we dismiss and then attempt to get video URL from the info dictionary provided.

It is a good idea to copy the video somewhere else and not hold onto the original URL.

And that is basic usage of UIImagePickerController to get video from user's library in Full HD or even 4K if we want to.

Thanks for reading!

Top comments (3)

Collapse
 
riccardoios profile image
Riccardoios

thank you for the awesome work, one question: How did you manage to arrive to write this code? picker.mediaTypes = ["public.movie"] if the answer is stackoverflow than my question is how did that person achieve to write that since there is no documentation on apple or books from apple that explain this? is it just through trial and error in order to figure out? many thanks

Collapse
 
sprocketus profile image
Ivan Zhuk

This was extremely helpful, thanks <3

Collapse
 
akshaykalucha3 profile image
Akshay

Thank you for the insight
Can you please upload the whole function as i am unable to correctly implement it correctly