UIImagePickerController 打开为 .camera 而不是 .savedPhotosAlbum

UIImagePickerController opening as .camera instead of .savedPhotosAlbum

我正在使用这两个单独的按钮来打开相机或 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")
    }
}

“相机”按钮正常工作,这意味着它可以打开相机,但画廊按钮也会打开相机。我有 double-checked 来确保画廊按钮实际链接到 tapGal 功能。我也试过用.photoLibrary替换.savedPhotosAlbum,但结果是一样的。

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

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

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

你的错误是你为这两种方法传递了同一行代码:

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