仅为AVCaptureVideoPreviewLayer禁用旋转

8

我有一个UITabBarController,其中包含4个选项卡。其中之一是一个AvCaptureVideoPreviewLayer(是条形码扫描器)。我想要做的是仅禁用AVCaptureVideoPreviewLayer的自动旋转(就像iOS相机应用程序一样),但不禁用item bar随着屏幕旋转。这是一个有点困难的情况,因为我认为UITabBarConrtoller不容易让你禁用旋转。

我的相机视图代码如下:

import UIKit
import AVFoundation

class ScannerViewController: UIViewController, 

// MARK: Properties

/// Manages the data flow from the capture stage through our input devices.
var captureSession: AVCaptureSession!

/// Dispays the data as captured from our input device.
var previewLayer: AVCaptureVideoPreviewLayer!


// MARK: Methods

override func viewDidLoad() {

    super.viewDidLoad()

    view.backgroundColor = UIColor.blackColor()
    self.captureSession = AVCaptureSession()

    // This object represents a physical capture device and the properties associated with that device
    let videoCaptureDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
    // Is useful for capturing the data from the input device
    let videoInput: AVCaptureDeviceInput

    do {
        videoInput = try AVCaptureDeviceInput(device: videoCaptureDevice)
    } catch {
        // TODO: maybe show an alert
        return
    }

    if (self.captureSession.canAddInput(videoInput)) {
        self.captureSession.addInput(videoInput)
    } else {
        failed();
        return;
    }

    let metadataOutput = AVCaptureMetadataOutput()

    if (self.captureSession.canAddOutput(metadataOutput)) {
        self.captureSession.addOutput(metadataOutput)

        metadataOutput.setMetadataObjectsDelegate(self, queue: dispatch_get_main_queue())
        metadataOutput.metadataObjectTypes = [AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypePDF417Code,
            AVMetadataObjectTypeUPCECode, AVMetadataObjectTypeCode39Code,
            AVMetadataObjectTypeCode39Mod43Code, AVMetadataObjectTypeCode93Code,
            AVMetadataObjectTypeCode128Code]
    } else {
        failed()
        return
    }

    // Adds the preview layer to display the captured data. Sets the videoGravity to AspectFill so that it covers the full screen
    self.previewLayer = AVCaptureVideoPreviewLayer(session: self.captureSession);
    self.previewLayer.frame = self.view.layer.bounds;
    self.previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
    self.view.layer.addSublayer(previewLayer);
    self.captureSession.startRunning();

}

override func viewDidLayoutSubviews() {

    self.previewLayer?.frame = self.view.layer.bounds;

}

override func didReceiveMemoryWarning() {

    super.didReceiveMemoryWarning()

}

override func viewWillAppear(animated: Bool) {

    super.viewWillAppear(animated)

    if (self.captureSession.running == false) {
        self.captureSession.startRunning();
    }

}

override func viewWillDisappear(animated: Bool) {

    super.viewWillDisappear(animated)

    if (self.captureSession.running == true) {
        self.captureSession.stopRunning();
    }

}

func failed() {

    let ac = UIAlertController(title: "Scanning not supported", message: "Your device does not support scanning a code from an item. Please use a device with a camera.", preferredStyle: .Alert)
    ac.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
    presentViewController(ac, animated: true, completion: nil)
    self.captureSession = nil

}

func captureOutput(captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [AnyObject]!, fromConnection connection: AVCaptureConnection!) {

    self.captureSession.stopRunning()

    if let metadataObject = metadataObjects.first {
        let readableObject = metadataObject as! AVMetadataMachineReadableCodeObject;

        AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate))
        foundCode(readableObject.stringValue);
    }

}

/// Completes some tasks when a barcode is found.
///
/// - parameter code: The the barcode found.
func foundCode(code: String) {

}

}
1个回答

2

我不确定自己是否完全理解了它。但你可以这样做:

1 - 子类化你的 UITabBarController 并重写 shouldAutorotate,例如:

override var shouldAutorotate: Bool {
    guard let viewController = tabBarController?.selectedViewController else { return false }
    return viewController.shouldAutorotate
}

这意味着所选的UIViewController将定义是否自动旋转。
2-现在UITabBarController将询问其ViewControllers是否应该自动旋转,您还需要在每个控制器中覆盖shouldAutorotate
就是这样!
如果我误解了您的问题,很抱歉。如果提供更多信息,将有助于解决问题。
保重 :)

1
很好!它帮助了我。 - Lucas Ferraz

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接