【问题标题】:UIImagePickerController opening as .camera instead of .savedPhotosAlbum [closed]UIImagePickerController 打开为 .camera 而不是 .savedPhotosAlbum [关闭]
【发布时间】:2023-03-04 09:27:01
【问题描述】:

我使用这两个单独的按钮来打开相机或 imageGallery。

@IBAction func tapGal(_ sender: Any) {
    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.savedPhotosAlbum) {
        let imagePicker = UIImagePickerController()
        imagePicker.delegate = self
        imagePicker.sourceType = UIImagePickerControllerSourceType.camera
        imagePicker.allowsEditing = true

        self.present(imagePicker, animated: true, completion: nil)

    } else {
        NSLog("No Cam Fam")
    }
}

@IBAction func tapCam(_ sender: Any) {
    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera) {
        let imagePicker = UIImagePickerController()
        imagePicker.delegate = self
        imagePicker.sourceType = UIImagePickerControllerSourceType.camera
        imagePicker.allowsEditing = true

        self.present(imagePicker, animated: true, completion: nil)

    } else {
        NSLog("No Cam Fam")
    }
}

“相机”按钮正常工作,这意味着它会打开相机,但图库按钮也会打开相机。我已经仔细检查以确保图库按钮实际上链接到 tapGal 函数。我也试过用.photoLibrary替换.savedPhotosAlbum,但结果是一样的。

【问题讨论】:

  • 请仔细查看您的代码。查看imagePicker.sourceType = ... 的两条线。
  • 你的代码是一样的,所以结果是一样的。

标签: ios swift camera uiimagepickercontroller gallery


【解决方案1】:

尝试用新方法替换您的方法。

您的错误是您为两种方法都传递了相同的代码行:

imagePicker.sourceType = UIImagePickerControllerSourceType.camera

代替

imagePicker.sourceType = UIImagePickerControllerSourceType.savedPhotosAlbum

用您的方法替换以下方法:-

@IBAction func tapGal(_ sender: Any) {
    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.savedPhotosAlbum) {
        let imagePicker = UIImagePickerController()
        imagePicker.delegate = self
        imagePicker.sourceType = UIImagePickerControllerSourceType.savedPhotosAlbum



        imagePicker.allowsEditing = true

        self.present(imagePicker, animated: true, completion: nil)

    } else {
        NSLog("No PhotoLibrary")
    }
}

【讨论】:

    【解决方案2】:
    @IBAction func tapGal(_ sender: Any) {
        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType. photoLibrary) {
            let imagePicker = UIImagePickerController()
            imagePicker.delegate = self
            imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary // use PhotoLibrary, not camera
            imagePicker.allowsEditing = true
    
            self.present(imagePicker, animated: true, completion: nil)
    
        } else {
            NSLog("No Cam Fam")
        }
    }
    
    @IBAction func tapCam(_ sender: Any) {
        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera) {
            let imagePicker = UIImagePickerController()
            imagePicker.delegate = self
            imagePicker.sourceType = UIImagePickerControllerSourceType.camera
            imagePicker.allowsEditing = true
    
            self.present(imagePicker, animated: true, completion: nil)
    
        } else {
            NSLog("No Cam Fam")
        }
    }
    

    【讨论】:

    • 纯代码的答案是不受欢迎的。请说明问题所在以及您的代码如何解决问题。这样可以得到更好的答案。
    【解决方案3】:

    @rmaddy 指出的简单修复。 我需要将imagePicker.sourceType = UIImagePickerControllerSourceType.camera 更改为imagePicker.sourceType = UIImagePickerControllerSourceType.savedPhotosAlbum

    【讨论】:

      猜你喜欢
      • 2020-02-11
      • 1970-01-01
      • 2019-11-15
      • 2018-07-15
      • 2023-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多