简体   繁体   English

照片库无法访问,只有相机选项可用 swift 代码

[英]Photo Library is not accessible only camera option available swift code

I am using Swift 5 code and in code I have to capture image.我正在使用 Swift 5 代码,并且在代码中我必须捕获图像。 My source type would be .camera or .photoLibrary .我的源类型是.camera.photoLibrary I have perfectly set everything even the permission settings in Info.plist but for unseen reason photo library is not accessible for selecting image.我已经完美地设置了所有内容,即使是Info.plist的权限设置,但由于看不见的原因,照片库无法用于选择图像。 Only camera option is available every time.每次都只有相机选项可用。 Please suggest and look into my code what am I doing wrong?请建议并查看我的代码我做错了什么?

Permission Info.plist:权限信息.plist:

Privacy - Photo Library Additions Usage Description
Privacy - Photo Library Usage Description
Privacy - Media Library Usage Description
Privacy - Camera Usage Description

Code:代码:

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

You always prioritize camera in your guard你总是优先考虑你的警卫中的相机

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

Change the argument type from ImageSource to UIImagePickerController.SourceType将参数类型从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)
    }

It should work now.它现在应该可以工作了。

Sorry, you seem to check whether the device have a camera and then proceed to select that.抱歉,您似乎在检查设备是否有摄像头,然后继续选择。 Show an action sheet for the user to select one of them.显示操作表供用户选择其中之一。 It would work.它会工作。

use this function:使用这个功能:

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

I think this function is shorter and reduces the extra code.我认为这个函数更短,减少了额外的代码。

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


        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM