gpt4 book ai didi

ios - 如何在预览 View 层上设置相机大小

转载 作者:行者123 更新时间:2023-11-28 09:55:57 25 4
gpt4 key购买 nike

我使用以下代码在 iPhone 设备上显示摄像头。相机可以显示用户界面,我可以拍照。但是,相机只显示在 previewLayer 的中间。如何让相机填满 AVCaptureVideoPreviewLayer 上的所有空间?我尝试使用 AVLayerVideoGravityResizeAspectFill 重力,但它会使图像变形。

self.session = AVCaptureSession()
do {
let device = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
try self.videoInput = AVCaptureDeviceInput(device: device)
} catch {

}
if ((self.session?.canSetSessionPreset(AVCaptureSessionPresetHigh)) != nil){
self.session?.sessionPreset = AVCaptureSessionPresetHigh
}
self.stillImageOutput = AVCaptureStillImageOutput()

self.stillImageOutput?.outputSettings = [AVVideoCodecKey:AVVideoCodecJPEG]
if self.session!.canAddInput(self.videoInput){
self.session?.addInput(self.videoInput)
}
if self.session!.canAddOutput(self.stillImageOutput){
self.session?.addOutput(self.stillImageOutput)
}

self.previewLayer = AVCaptureVideoPreviewLayer(session: self.session)
//self.backView.frame = CGRectMake(0, 0, self.view.bounds.width,self.view.bounds.width)
let viewLayer:CALayer = self.backView.layer
viewLayer.masksToBounds = true
let bounds:CGRect = viewLayer.bounds
self.previewLayer?.frame = bounds
self.previewLayer?.backgroundColor = UIColor.blackColor().CGColor
self.previewLayer?.videoGravity = AVLayerVideoGravityResizeAspect//AVLayerVideoGravityResizeAspect
self.backView.layer.addSublayer(self.previewLayer!)

下面是相机的截图。

enter image description here

最佳答案

我这样做:

override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
videoPreviewLayer?.frame = previewView.layer.bounds
}

如果您在 viewDidLoad() 中执行此操作,您可能会得到错误的边界。

关于ios - 如何在预览 View 层上设置相机大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37098084/

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