gpt4 book ai didi

ios - 照片库无法访问只有相机选项可用的快速代码

转载 作者:行者123 更新时间:2023-11-28 18:46:03 25 4
gpt4 key购买 nike

我正在使用 Swift 5 代码,在代码中我必须捕获图像。我的源类型是 .camera.photoLibrary。我已经完美地设置了所有内容,甚至是 Info.plist 中的权限设置,但由于不明原因无法访问照片库以选择图像。每次只有相机选项可用。请建议并查看我的代码我做错了什么?

权限信息.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)
}

最佳答案

在你的守卫中,你总是优先考虑摄像头

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

关于ios - 照片库无法访问只有相机选项可用的快速代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59370760/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com