UIImagePicker源类型并不想改变

时间:2016-11-10 17:21:15

标签: ios swift camera uiimagepickercontroller

我已经设置了一个带有.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>

但是当我试图拍照时,它一直向我展示我的相册,这就是问题所在。 我希望能够从手机的相机拍照。

1 个答案:

答案 0 :(得分:1)

您需要输入此代码

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

viewDidLoadcameraTapped

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