我有一个 tabBar 应用程序。其中一个选项卡有一个 AVCaptureVideoPreviewLayer,我在其中显示了一个用于读取条形码的相机。当我旋转屏幕时,所有内容都会旋转(选项卡栏和相机图像)。我想要得到的是相机的静态图像(禁用其旋转)和旋转屏幕时旋转的选项卡栏。
这就是我将相机图层添加到 tabBar 的方法:
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 {
self.failed();
return
}
if (self.captureSession.canAddInput(videoInput)) {
self.captureSession.addInput(videoInput)
} else {
self.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 {
self.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(self.previewLayer)
self.captureSession.startRunning();
}
最佳答案
停止 View 而不是图层的旋转。
override func shouldAutorotate() -> Bool {
return false
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return UIInterfaceOrientation.Portrait.rawValue
}
这应该有效。
关于ios - 强制图层不在选项卡栏中旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35269701/