相机没有响应

时间:2016-12-06 18:42:07

标签: ios swift camera

请查看我的代码,我没有任何错误,但是当我打开我的视图控制器时,相机无法启动,只有黑屏,即使按钮也没有出现。

import UIKit
import AVFoundation

class CameraViewController: UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate {


let picker = UIImagePickerController()
@IBOutlet weak var myImageView: UIImageView!
@IBOutlet weak var cameraView: UIView!

    @IBAction func photoLibrary(_ sender: UIBarButtonItem) {

    picker.allowsEditing = false
    picker.sourceType = .photoLibrary
    picker.mediaTypes = UIImagePickerController.availableMediaTypes(for: .photoLibrary)!
    picker.modalPresentationStyle = .popover
    present(picker, animated: true, completion: nil)
    picker.popoverPresentationController?.barButtonItem = sender
}

@IBAction func shootPhoto(_ sender: UIBarButtonItem) {

    if UIImagePickerController.isSourceTypeAvailable(.camera) {
        picker.allowsEditing = false
        picker.sourceType = UIImagePickerControllerSourceType.camera
        picker.cameraCaptureMode = .photo
        picker.modalPresentationStyle = .fullScreen
        present(picker,animated: true,completion: nil)

    } else {
        noCamera()
    }
}
func noCamera(){
    let alertVC = UIAlertController(
        title: "No Camera",
        message: "Sorry, this device has no camera",
        preferredStyle: .alert)
    let okAction = UIAlertAction(
        title: "OK",
        style:.default,
        handler: nil)
    alertVC.addAction(okAction)
    present(
        alertVC,
        animated: true,
        completion: nil)
}
override func viewDidLoad() {
    super.viewDidLoad()
    picker.delegate = self
}

//MARK: - Delegates
private func imagePickerController(_ picker: UIImagePickerController,
                           didFinishPickingMediaWithInfo info: [String : AnyObject])
{
    var  chosenImage = UIImage()
    chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage //2
    myImageView.contentMode = .scaleAspectFit //3
    myImageView.image = chosenImage //4
    dismiss(animated:true, completion: nil) //5
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
    dismiss(animated: true, completion: nil)
}

 }

我只是完全黑屏,甚至没有来自Swift的提示。我现在不知道该怎么做。

1 个答案:

答案 0 :(得分:0)

如果您尚未设置初始viewcontroller,则会发生这种情况。请确保首先加载的视图控件在故事板中设置为初始viewcontroller。

enter image description here