如何使用UIImagePickerControllerDelegate在viewDidLoad上启动摄像机? [英] How to launch camera on viewDidLoad with UIImagePickerControllerDelegate?

查看:202
本文介绍了如何使用UIImagePickerControllerDelegate在viewDidLoad上启动摄像机?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Swift中有一个新的iOS应用程序,我试图在应用程序启动时显示相机。在我的视图控制器我有一个单一的 UIView 对象,我尝试填充相机。

I have a new iOS app in Swift where I'm trying to show the camera as soon as the app is launched. In my view controller I have a single UIView object which I try to fill with the camera.

我的视图控制器调用 viewDidLoad 方法中的 UIImagePickerController ,但没有任何反应。

The code in my view controller calls UIImagePickerController in the viewDidLoad method but nothing happens.

这里是代码,任何想法为什么没有发生什么?是不是相机应该打开应用程序启动与这段代码?

Here is the code, any idea why nothing happens? Isn't the camera supposed to open on app launch with this code?

import UIKit

class ViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
    let imagePicker = UIImagePickerController()
    @IBOutlet weak var imageViewer: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()
        if UIImagePickerController.isCameraDeviceAvailable(   UIImagePickerControllerCameraDevice.Front) {
            imagePicker.delegate = self
            imagePicker.sourceType = UIImagePickerControllerSourceType.Camera
            presentViewController(imagePicker, animated: true, completion: nil)
        } else {
            println("no front camera available")
        }

        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
        dismissViewControllerAnimated(true, completion: nil)
        imageViewer.image = image
    }
}

:如果我添加一个按钮到故事板,将它连接到代码,并把来自 viewDidLoad 的代码,相机在点击按钮时工作:

UPDATE: if I add a button to the storyboard, connect it to the code and put the code from viewDidLoad in it, the camera works when the button is clicked:

@IBAction func presentImagePicker(sender: AnyObject) {

    if UIImagePickerController.isCameraDeviceAvailable( UIImagePickerControllerCameraDevice.Front) {
        imagePicker.delegate = self
        imagePicker.sourceType = UIImagePickerControllerSourceType.Camera
        presentViewController(imagePicker, animated: true, completion: nil)
    }
}

我知道摄像机工作,但是如何让它工作,而不必按下按钮?

I know that the camera works, but how to make it work without having to press a button?

推荐答案

这里是你的解决方案。

c $ c> viewDidAppear 方法:

Put your code in viewDidAppear method this way:

override func viewDidAppear(animated: Bool) {

    if UIImagePickerController.isCameraDeviceAvailable( UIImagePickerControllerCameraDevice.Front) {
        imagePicker.delegate = self
        imagePicker.sourceType = UIImagePickerControllerSourceType.Camera
        presentViewController(imagePicker, animated: true, completion: nil)
    }
}

尝试在viewDidLoad中执行此操作将无法工作,因为在首次创建视图后调用该视图,并且视图不是该视图的子视图点。

Trying to do it in viewDidLoad won't work because that is called after the view is first created, and the view isn't a subview of anything else at that point.

这篇关于如何使用UIImagePickerControllerDelegate在viewDidLoad上启动摄像机?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆