我正在使用Swift 5代码,并且在代码中我必须捕获图像。我的来源类型为.camera
或.photoLibrary
。我已经完美地设置了所有内容,甚至包括Info.plist
中的权限设置,但是由于不明原因,无法使用照片库选择图像。每次都只有相机选项可用。请提出建议并查看我的代码,我在做什么错了?
权限信息列表:
Privacy - Photo Library Additions Usage Description
Privacy - Photo Library Usage Description
Privacy - Media Library Usage Description
Privacy - Camera Usage Description
代码:
var imagePicker: UIImagePickerController!
enum ImageSource {
case photoLibrary
case camera
}
//MARK: - Take image
func takePhoto() {
guard UIImagePickerController.isSourceTypeAvailable(.camera) else {
selectImageFrom(.photoLibrary)
return
}
selectImageFrom(.camera)
}
func selectImageFrom(_ source: ImageSource){
imagePicker = UIImagePickerController()
imagePicker.delegate = self
switch source {
case .camera:
imagePicker.sourceType = .camera
case .photoLibrary:
imagePicker.sourceType = .photoLibrary
}
present(imagePicker, animated: true, completion: nil)
}
答案 0 :(得分:3)
您始终会优先保护摄像机
//MARK: - Take image
func takePhotoLib() {
guard UIImagePickerController.isSourceTypeAvailable(.photoLibrary) else {
return
}
selectImageFrom(.photoLibrary)
}
//MARK: - Take image
func takePhotoCamera() {
guard UIImagePickerController.isSourceTypeAvailable(.camera) else {
return
}
selectImageFrom(.camera)
}
答案 1 :(得分:1)
将参数类型从andThen
更改为ImageSource
UIImagePickerController.SourceType
现在应该可以工作。
对不起,您似乎正在检查设备是否有摄像头,然后继续进行选择。显示一个操作表,供用户选择其中之一。会的。
使用此功能:
func selectImageFrom(_ source: UIImagePickerController.SourceType){
imagePicker = UIImagePickerController()
imagePicker.delegate = self
switch source {
case .camera:
imagePicker.sourceType = .camera
case .photoLibrary:
imagePicker.sourceType = .photoLibrary
}
present(imagePicker, animated: true, completion: nil)
}
答案 2 :(得分:0)
我认为此功能更短,并减少了额外的代码。
func selectImageFrom(_ source: UIImagePickerController.SourceType) {
guard UIImagePickerController.isSourceTypeAvailable(source) else {
return
}
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = source
present(imagePicker, animated: true, completion: nil)
}
答案 3 :(得分:0)
func selectimage(_ sender: UIButton)
{
let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
alert.addAction(UIAlertAction(title: "Take Photo", style: .default, handler: { _ in
self.openCamera()
}))
alert.addAction(UIAlertAction(title: "Choose Photo", style: .default, handler: { _ in
self.openGallary()
}))
alert.addAction(UIAlertAction.init(title: "Cancel", style: .cancel, handler: nil))
//If you want work actionsheet on ipad then you have to use popoverPresentationController to present the actionsheet, otherwise app will crash in iPad
switch UIDevice.current.userInterfaceIdiom {
case .pad:
alert.popoverPresentationController?.sourceView = sender
alert.popoverPresentationController?.sourceRect = sender.bounds
alert.popoverPresentationController?.permittedArrowDirections = .up
default:
break
}
self.present(alert, animated: true, completion: nil)
}
//MARK: - Open the camera
func openCamera(){
if(UIImagePickerController .isSourceTypeAvailable(UIImagePickerControllerSourceType.camera)){
imagePicker.sourceType = UIImagePickerControllerSourceType.camera
//If you dont want to edit the photo then you can set allowsEditing to false
// imagePicker.allowsEditing = true
imagePicker.delegate = self
imagePicker.modalPresentationStyle = .overCurrentContext
self.present(imagePicker, animated: true, completion: nil)
}
else{
let alert = UIAlertController(title: "Warning", message: "You don't have camera", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
}
func openGallary(){
if(UIImagePickerController .isSourceTypeAvailable(UIImagePickerControllerSourceType.photoLibrary)){
imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary
self.tabBarController?.tabBar.isHidden = true
//If you dont want to edit the photo then you can set allowsEditing to false
// imagePicker.allowsEditing = true
imagePicker.delegate = self
imagePicker.modalPresentationStyle = .overCurrentContext
self.present(imagePicker, animated: true, completion: nil)
}