when using the PhotoPicker
or UIImage
, I can't restrict the picker to show only images, and excluding videos and GIFs.
struct ImagePickerView: UIViewControllerRepresentable {
func makeUIViewController(context: Context) -> UIImagePickerController {
let imagePicker = UIImagePickerController()
imagePicker.delegate = context.coordinator
// Filter to display only images (still includes GIFs)
imagePicker.mediaTypes = [UTType.image.identifier]
return imagePicker
}
func makeCoordinator() -> Coordinator {
return Coordinator()
}
class Coordinator: NSObject, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let selectedImageURL = info[.imageURL] as? URL {
// Check if the selected file is a GIF
if selectedImageURL.pathExtension.lowercased() == "gif" {
print("GIFs are not allowed.")
picker.dismiss(animated: true, completion: nil)
return
}
}
if let selectedImage = info[.originalImage] as? UIImage {
// Process the selected image
print(selectedImage)
}
picker.dismiss(animated: true, completion: nil)
}
}
func updateUIViewController(_ uiViewController: UIImagePickerController, context: Context) {
}
}
I’m using UIImagePickerController
in Swift to allow users to select images, but it still shows GIFs. I want to completely exclude GIFs from being displayed in the picker.
I found out that GIFs are categorized as images on iOS.
I don't think this is possible using the picker.
As someone else suggested in the comments, you're better off building your own photo picker and fetching the photos using Apple's PhotoKit framework.