6.8 C
New York
Friday, November 15, 2024

ios – How can I filter the PhotoPicker to solely present photographs, excluding movies and GIFs?


when utilizing the PhotoPicker or UIImage, I can not prohibit the picker to indicate solely photographs, and excluding movies and GIFs.

struct ImagePickerView: UIViewControllerRepresentable {
    
    func makeUIViewController(context: Context) -> UIImagePickerController {
        let imagePicker = UIImagePickerController()
        imagePicker.delegate = context.coordinator
        
        // Filter to show solely photographs (nonetheless consists of GIFs)
        imagePicker.mediaTypes = [UTType.image.identifier]
        return imagePicker
    }
    
    func makeCoordinator() -> Coordinator {
        return Coordinator()
    }
    
    class Coordinator: NSObject, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
        
        func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo data: [UIImagePickerController.InfoKey : Any]) {
            if let selectedImageURL = data[.imageURL] as? URL {
                // Test if the chosen file is a GIF
                if selectedImageURL.pathExtension.lowercased() == "gif" {
                    print("GIFs usually are not allowed.")
                    picker.dismiss(animated: true, completion: nil)
                    return
                }
            }
            
            if let selectedImage = data[.originalImage] as? UIImage {
                // Course of the chosen picture
                print(selectedImage)
            }
            picker.dismiss(animated: true, completion: nil)
        }
    }
    
    func updateUIViewController(_ uiViewController: UIImagePickerController, context: Context) {
    }
}

I’m utilizing UIImagePickerController in Swift to permit customers to pick photographs, nevertheless it nonetheless reveals GIFs. I wish to fully exclude GIFs from being displayed within the picker.

I came upon that GIFs are categorized as photographs on iOS.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles