【问题标题】:Photo Library is not accessible only camera option available swift code照片库无法访问,只有相机选项可用 swift 代码
【发布时间】:2019-12-17 09:03:38
【问题描述】:

我正在使用 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)
    }

【问题讨论】:

    标签: ios swift uiimagepickercontroller


    【解决方案1】:

    你总是优先考虑摄像头

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

    【讨论】:

    • 谢谢我已经尝试实现你的方式,但每次photoLibrary 可以访问但没有camera 但是如果我将切换源语句更改为imagePicker.sourceType = source 它会给我错误。
    【解决方案2】:
          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)
    
    
            }
    

    【讨论】:

      【解决方案3】:

      我认为这个函数更短,减少了额外的代码。

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

      【讨论】:

        【解决方案4】:

        将参数类型从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)
            }
        
        

        它现在应该可以工作了。

        抱歉,您似乎在检查设备是否有摄像头,然后继续选择它。显示一个操作表供用户选择其中一个。它会起作用的。

        使用此功能:

        func showOptions(sender: AnyObject) {
            let alertController = UIAlertController.init(title: "Choose Option", message: "", preferredStyle: .actionSheet)
            let cameraAction = UIAlertAction.init(title: "Camera", style: .default) { (action) in
                self.selectImageFrom(.camera)
            }
            alertController.addAction(cameraAction)
            let photoLibraryAction = UIAlertAction.init(title: "Photo Library", style: .default) { (action) in
                self.selectImageFrom(.photoLibrary)
            }
            alertController.addAction(photoLibraryAction)
            let cancelAction = UIAlertAction.init(title: "Cancel", style: .cancel) { (action) in
            }
            alertController.addAction(cancelAction)
            present(alertController, animated: true, completion: nil)
        }
        

        【讨论】:

        • 谢谢。我更新了您的代码argument type,但仍然只有相机可用,没有照片库。在相机屏幕上,它仅显示取消、用于照片和相机交换正面或背面的中间按钮。没有从库中选择照片的选项。
        • 对不起@NewBieMobile,您似乎检查了设备是否有摄像头,然后继续选择它。显示一个操作表供用户选择其中一个。它会起作用的。
        • 如果你只是删除你会得到的保护声明。您正在检查相机是否可用,然后选择它并返回,因此无法访问照片库。
        • 我正在检查我的手机,相机工作正常,但不适用于照片库。但是如果我删除了相机选项.photolibrary 工作。
        • @NewBieMobile 很高兴能帮上忙。 :)
        猜你喜欢
        • 2019-02-23
        • 1970-01-01
        • 2017-10-12
        • 1970-01-01
        • 2014-12-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多