UIImagePicker 源类型不想改变

UIImagePicker source type doesn't want to change

为了开发应用程序,我设置了一个带有 .savedPhotosAlbum 作为源类型的 UIImagePicker。

import UIKit

class PictureViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

    /////////////////// OUTLETS ////////////////////
    ///////////////// PROPERTIES ///////////////////
    var imagePicker = UIImagePickerController()

override func viewDidLoad() {
        super.viewDidLoad()

        imagePicker.delegate = self

    }

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        // Where do we want our photos from and we don't want edited photos
        imagePicker.allowsEditing = false
        imagePicker.sourceType = UIImagePickerControllerSourceType.camera
        imagePicker.cameraCaptureMode = .photo
        imagePicker.modalPresentationStyle = .fullScreen

        // Let's take the image from the picker
        let image = info[UIImagePickerControllerOriginalImage] as! UIImage
        snapImage.image = image

        // When the user picks a picture, disabling the background color of the UIImageView
        snapImage.backgroundColor = UIColor.clear

        // Closing the image picker
        imagePicker.dismiss(animated: true, completion: nil)
    }

    @IBAction func cameraTapped(_ sender: Any) {
        present(imagePicker, animated: true, completion: nil)
    }

现在我的应用已经完成,我想在我的 Iphone 上试用我的应用,所以我更改了这个:

imagePicker.sourceType = .savedPhotosAlbum

imagePicker.sourceType = UIImagePickerControllerSourceType.camera

并且我在我的 info.plist

中添加了
<key>NSCameraUsageDescription</key>
    <string>We want to access your camera to ...</string>

但是当我尝试拍照时,它一直显示我的相册,这就是问题所在。 我希望能够用我的 phone.

的相机拍照

您需要输入此代码

imagePicker.allowsEditing = false
imagePicker.sourceType = UIImagePickerControllerSourceType.camera
imagePicker.cameraCaptureMode = .photo
imagePicker.modalPresentationStyle = .fullScreen

viewDidLoadcameraTapped

问题 - 现在您将这段代码放在 UIImagePicker 的委托方法中,该方法在选择图像或拍摄图像后被调用。