iOS: How to let user select video from their library

nemecek_f profile image Filip Němeček ・2 min read

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!

Posted on Feb 13 by:

nemecek_f profile

Filip Němeček


Primarily iOS developer, I also like Django and Python. And dabble with JavaScript occasionally. Love reading and coffee.


markdown guide