viewWillAppear(再次)重新初始化条形码阅读器

时间:2015-04-13 11:59:30

标签: ios swift video view

我有一个条形码阅读器,可以在第一次加载视图时正确初始化。但是,当我返回视图时,视频输入停止工作。

下面的代码显示了如何在viewDidLoad方法中初始化。关于如何修改它的任何建议,以便我可以在viewWillAppear(再次)时调用它的一部分?

代码:

override func viewDidLoad() {
    super.viewDidLoad()

    // Get an instance of the AVCaptureDevice class to initialize a device object and provide the video
    // as the media type parameter.
    let captureDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)

    // Get an instance of the AVCaptureDeviceInput class using the previous device object.
    var error:NSError?
    let input: AnyObject! = AVCaptureDeviceInput.deviceInputWithDevice(captureDevice, error: &error)

    if (error != nil) {
        // If any error occurs, simply log the description of it and don't continue any more.
        println("\(error?.localizedDescription)")
        return
    }

    // Initialize the captureSession object.
    captureSession = AVCaptureSession()
    // Set the input device on the capture session.
    captureSession?.addInput(input as AVCaptureInput)

    // Initialize a AVCaptureMetadataOutput object and set it as the output device to the capture session.
    let captureMetadataOutput = AVCaptureMetadataOutput()
    captureSession?.addOutput(captureMetadataOutput)

    // Set delegate and use the default dispatch queue to execute the call back
    captureMetadataOutput.setMetadataObjectsDelegate(self, queue: dispatch_get_main_queue())
    captureMetadataOutput.metadataObjectTypes = supportedBarCodes

    // Initialize the video preview layer and add it as a sublayer to the viewPreview view's layer.
    videoPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
    //videoPreviewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill

    var tmpbounds=view.layer.bounds;

    videoPreviewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill;
    videoPreviewLayer?.bounds = tmpbounds
    videoPreviewLayer?.position = CGPointMake(CGRectGetMidX(tmpbounds), CGRectGetMidY(tmpbounds))

    // videoPreviewLayer?.videoGravity = AVLayerVideoGravityResizeAspect

    videoPreviewLayer?.frame = view.layer.bounds
    view.layer.addSublayer(videoPreviewLayer)

    // Start video capture.
    captureSession?.startRunning()

    // Move the message label to the top view
    view.bringSubviewToFront(messageLabel)

    // Initialize QR Code Frame to highlight the QR code
    qrCodeFrameView = UIView()
    qrCodeFrameView?.layer.borderColor = UIColor.greenColor().CGColor
    qrCodeFrameView?.layer.borderWidth = 2
    view.addSubview(qrCodeFrameView!)
    view.bringSubviewToFront(qrCodeFrameView!)
}

2 个答案:

答案 0 :(得分:1)

尝试在viewDidAppear上执行

captureSession?.startRunning()

on viewWillDissapear

captureSession?.stopRunning()

同时注册通知 AVCaptureSessionRuntimeErrorNotification 并记录所有错误。我想它会帮助你理解出了什么问题。

答案 1 :(得分:0)

您正在viewDidLoad中设置captureSession和videoPreviewLayer。这仅在创建ViewController时运行。您需要将一些代码移动到viewDidAppear,每次出现此视图时都会运行。最简单的是,从这一行移动所有代码:

captureSession = AVCaptureSession()

开始进入viewDidAppear