NSInvalidArgumentException', reason: 'The Live Photo type cannot be specified without the Image media type'

94 Views Asked by At

I'm trying to fetch only Live Photos from my gallery for processing tasks. However, the below code gives a runtime crash:

        let imagePicker = UIImagePickerController()
        imagePicker.sourceType = .photoLibrary
        //imagePicker.mediaTypes = [kUTTypeImage, kUTTypeLivePhoto] as [String]
        imagePicker.mediaTypes = [kUTTypeLivePhoto] as [String]
        imagePicker.delegate = self
        present(imagePicker, animated: true, completion: nil)

Error: NSInvalidArgumentException', reason: 'The Live Photo type cannot be specified without the Image media type'

Coincidentally, using PHPhotosPicker with SwiftUI (iOS 16+) successfully fetches live photos only:

PhotosPicker(selection: $viewModel.imageSelection,
                             matching: .livePhotos,
                             photoLibrary: .shared())

So, curious to know what's wrong with the former code?
1

There are 1 best solutions below

0
HangarRash On

The documentation for UIImagePickerController states:

To obtain the full motion and sound content when the user chooses a Live Photo with the image picker, you must include both the kUTTypeImage and kUTTypeLivePhoto types in the mediaTypes array.

But more importantly, the .photoLibrary source type was deprecated in iOS 14.0. The documentation states:

Deprecated
Use PHPickerViewController instead.

Only use UIImagePickerController to take photos with the camera. For selecting existing media, use PHPickerViewController. It has many more useful features. And it supports what you want in this case.