ios - 有什么方法可以通过 UIImagePIcker 只选择“JPG”、“PNG”和“JPEG”图像
问题描述
self.imagePicker.mediaTypes = [(kUTTypeImage as String)]
通过使用时,UIImagePicker
我可以选择所有类型的图像,但我的要求是仅JPG, JPEG and PNG
通过照片库选择类型图像,GIF
并且HEIC
(实时照片)是不允许的。
那么,有什么方法可以防止选择GIF和HEIC类型的照片,而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)
}
}
}
}))
解决方案
您必须提及所需的唯一格式的标识符。
解决此问题的步骤
导入 MobileCoreServices
取一个 ImagePickerController 的变量,让我们说
let picker = UIImagePickerController()
指定媒体类型如下
picker.mediaTypes = [(kUTTypePNG as String), (kUTTypeJPEG as String), (kUTTypeImage as String)] // This is an array - you can add other format Strings as well
要进行交叉验证,您将看到视频/GIF 如果未作为媒体类型提及,则不会显示给应用用户。
推荐阅读
- python - 从 Dictionary 创建时使用 SQLalchemy 声明的基本对象的问题
- php - 我应该如何在 Laravel 中构建我的路由和控制器?
- java - 如何使用 Java 中的迭代器从通用数组列表中打印元素
- vue.js - Vue JS:从对象发送 axios 发布数据
- android - 带有令牌的 Android 架构 auth 和 api 层
- flask - 外部链接重定向到本地主机 [flask]
- sql - 将数据导入 DB2 数据库时出错
- python - Python:如何使用 3 个数组 x[]、y[]、z[] 在 3D 中应用 polyfit 功能
- dart - 如何对内部和外部地图进行排序?
- ionic-framework - 离子原生推送通知(Firebase X)不显示应用程序是否在后台