gpt4 book ai didi

ios - 无法为 AVCaptureVideoPreviewLayer 设置边界

转载 作者:行者123 更新时间:2023-11-28 07:48:40 25 4
gpt4 key购买 nike

我很难更改 AVCaptureVideoPreviewLayer 的边界。无论我做什么,它总是以小尺寸结束,如下所示:

enter image description here

这是我的代码:

override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
self.imageLayer.bounds = CGRect(x: 0, y: 0, width: 375, height: 100)
}

private func startLiveVideo() {

session.sessionPreset = AVCaptureSession.Preset.photo
let captureDevice = AVCaptureDevice.default(for: AVMediaType.video)

let deviceInput = try! AVCaptureDeviceInput(device: captureDevice!)
let deviceOutput = AVCaptureVideoDataOutput()
deviceOutput.videoSettings = [kCVPixelBufferPixelFormatTypeKey as String: Int(kCVPixelFormatType_32BGRA)]
deviceOutput.setSampleBufferDelegate(self, queue: DispatchQueue.global(qos: DispatchQoS.QoSClass.default))
session.addInput(deviceInput)
session.addOutput(deviceOutput)

self.imageLayer = AVCaptureVideoPreviewLayer(session: session)
imageLayer.frame = CGRect(x: 0, y: 0, width: 375, height: 100)
self.imageView.layer.addSublayer(self.imageLayer)

session.startRunning()
}

解决方案:

self.imageLayer.videoGravity = .resizeAspectFill

最佳答案

您可以首先从 Storyboard 或通过 yourViewController 分配 imageView 的宽度和高度 500 x 500 或您想要的任何大小。

imageLayer.frame = CGRect(x: 0, y: 0, width: self.imageView.frame.width, height: self.imageView.frame.height)

self.imageView.layer.addSublayer(self.imageLayer)

关于ios - 无法为 AVCaptureVideoPreviewLayer 设置边界,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50279227/

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