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?
The documentation for
UIImagePickerControllerstates:But more importantly, the
.photoLibrarysource type was deprecated in iOS 14.0. The documentation states:Only use
UIImagePickerControllerto take photos with the camera. For selecting existing media, usePHPickerViewController. It has many more useful features. And it supports what you want in this case.