gpt4 book ai didi

ios - AVCaptureVideoPreviewLayer 方向 - 需要横向

转载 作者:IT王子 更新时间:2023-10-29 07:34:53 24 4
gpt4 key购买 nike

我的应用只有横向。我正在像这样呈现 AVCaptureVideoPreviewLayer:

self.previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
[self.previewLayer setBackgroundColor:[[UIColor blackColor] CGColor]];
[self.previewLayer setVideoGravity:AVLayerVideoGravityResizeAspect];
NSLog(@"previewView: %@", self.previewView);
CALayer *rootLayer = [self.previewView layer];
[rootLayer setMasksToBounds:YES];
[self.previewLayer setFrame:[rootLayer bounds]];
NSLog(@"previewlayer: %f, %f, %f, %f", self.previewLayer.frame.origin.x, self.previewLayer.frame.origin.y, self.previewLayer.frame.size.width, self.previewLayer.frame.size.height);
[rootLayer addSublayer:self.previewLayer];
[session startRunning];

self.previewView的frame为(0,0,568,320),是正确的。 self.previewLayer 记录帧 (0,0,568,320),这在理论上是正确的。但是,相机显示在横向屏幕中间显示为纵向矩形,并且相机预览图像的方向错了 90 度。我究竟做错了什么?我需要相机预览层以全屏、横向模式显示,并且图像的方向应该正确。

最佳答案

swift 5.5,Xcode 13.2

private func updatePreviewLayer(layer: AVCaptureConnection, orientation: AVCaptureVideoOrientation) {
layer.videoOrientation = orientation
self.previewLayer?.frame = view.bounds
}

override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()

if let connection = self.previewLayer?.connection {
let currentDevice = UIDevice.current
let orientation: UIDeviceOrientation = currentDevice.orientation
let previewLayerConnection: AVCaptureConnection = connection

if previewLayerConnection.isVideoOrientationSupported {
switch orientation {
case .portrait: self.updatePreviewLayer(layer: previewLayerConnection, orientation: .portrait)
case .landscapeRight: self.updatePreviewLayer(layer: previewLayerConnection, orientation: .landscapeLeft)
case .landscapeLeft: self.updatePreviewLayer(layer: previewLayerConnection, orientation: .landscapeRight)
case .portraitUpsideDown: self.updatePreviewLayer(layer: previewLayerConnection, orientation: .portraitUpsideDown)
default: self.updatePreviewLayer(layer: previewLayerConnection, orientation: .portrait)
}
}
}
}

swift 2.2,Xcode 7.3

private func updatePreviewLayer(layer: AVCaptureConnection, orientation: AVCaptureVideoOrientation) {

layer.videoOrientation = orientation

previewLayer.frame = self.view.bounds

}

override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()

if let connection = self.previewLayer?.connection {

let currentDevice: UIDevice = UIDevice.currentDevice()

let orientation: UIDeviceOrientation = currentDevice.orientation

let previewLayerConnection : AVCaptureConnection = connection

if (previewLayerConnection.supportsVideoOrientation) {

switch (orientation) {
case .Portrait: updatePreviewLayer(previewLayerConnection, orientation: .Portrait)

case .LandscapeRight: updatePreviewLayer(previewLayerConnection, orientation: .LandscapeLeft)

case .LandscapeLeft: updatePreviewLayer(previewLayerConnection, orientation: .LandscapeRight)

case .PortraitUpsideDown: updatePreviewLayer(previewLayerConnection, orientation: .PortraitUpsideDown)

default: updatePreviewLayer(previewLayerConnection, orientation: .Portrait)

}
}
}
}

关于ios - AVCaptureVideoPreviewLayer 方向 - 需要横向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15075300/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com