首页 > 解决方案 > 有什么方法可以通过 UIImagePIcker 只选择“JPG”、“PNG”和“JPEG”图像

问题描述

self.imagePicker.mediaTypes = [(kUTTypeImage as String)]通过使用时,UIImagePicker我可以选择所有类型的图像,但我的要求是仅JPG, JPEG and PNG通过照片库选择类型图像,GIF并且HEIC(实时照片)是不允许的。

那么,有什么方法可以防止选择GIFHEIC类型的照片,而imagePicker.sourceType = .photoLibrary

代码:

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)
                    }
                }
            }

        }))

标签: iosobjective-cswiftxcodeuiimagepickercontroller

解决方案


您必须提及所需的唯一格式的标识符。

解决此问题的步骤

  1. 导入 MobileCoreServices

  2. 取一个 ImagePickerController 的变量,让我们说

    let picker = UIImagePickerController()
    
  3. 指定媒体类型如下

    picker.mediaTypes = [(kUTTypePNG as String), (kUTTypeJPEG as String), (kUTTypeImage as String)] // This is an array - you can add other format Strings as well
    

要进行交叉验证,您将看到视频/GIF 如果未作为媒体类型提及,则不会显示给应用用户。


推荐阅读