Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way by which I can select only "JPG", "PNG" and "JPEG" images through UIImagePIcker

While using self.imagePicker.mediaTypes = [(kUTTypeImage as String)] through UIImagePicker I am able to select all types of images but my requirement is select only JPG, JPEG and PNG type images through photo library and GIF and HEIC(live photos) are not allowed.

So, Is there any way by which I can prevent selecting GIFs and HEIC type photo while imagePicker.sourceType = .photoLibrary

Code:

imagePicker.delegate = self

        let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)

        actionSheet.addAction(UIAlertAction(title: StringConstants.Actions.Camera, style: .default, handler: { (action:UIAlertAction) in
            CheckPermissions.checkPermission(permissionFor: .Camera,vc:self) { (success) in
                if success {
                    DispatchQueue.main.async {[unowned self] in
                        if(UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera)) {
                            self.view.endEditing(true)
                            self.imagePicker.allowsEditing = false
                            self.imagePicker.sourceType = UIImagePickerControllerSourceType.camera
                            self.imagePicker.cameraCaptureMode = .photo
                            self.imagePicker.showsCameraControls = true
                            self.imagePicker.modalPresentationStyle = .overFullScreen
                            self.present(self.imagePicker, animated: true, completion: nil)
                        }
                        else {
                            self.showAlert(withTitle: kEmptyString, message: StringConstants.AlertMessage.cameraAlertMessage, andOkAction: nil )
                        }
                    }
                }
            }
        }))

        actionSheet.addAction(UIAlertAction(title: StringConstants.Actions.PhotoLibrary, style: .default, handler: { (action:UIAlertAction) in
            CheckPermissions.checkPermission(permissionFor: .PhotoLibrary,vc: self) { (success) in
                if success {
                    DispatchQueue.main.async {[unowned self] in
                        self.view.endEditing(true)
                        self.imagePicker.allowsEditing = false
                        self.imagePicker.sourceType = .photoLibrary
                        self.imagePicker.modalPresentationStyle = .overFullScreen
                        self.imagePicker.navigationBar.isTranslucent = false
                        self.imagePicker.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor : UIColor.white]
                        self.present(self.imagePicker, animated: true, completion: nil)
                    }
                }
            }

        }))
like image 537
Dhaval Kansara Avatar asked Nov 07 '22 10:11

Dhaval Kansara


1 Answers

Simply No. There is no way to give input of specific formate to UIImagePicker and get the result of only that formate.

UIImagePicker only accepts kUTTypeMovie and kUTTypeImage as a mediaType. What you can do is check the image/file extension after picking any image in didFinishPickingMediaWithInfo.

like image 83
Maulik Pandya Avatar answered Nov 14 '22 21:11

Maulik Pandya