Ios 如何使用AVCaptureVideoPreviewLayer从照相机应用程序实现2倍变焦
我的应用程序中有一个AVCaptureVideoPreviewLayer,运行良好,显示的预览视频与camera应用程序相同。我想实现相机应用程序的2倍变焦功能。我该怎么做 基本上,我希望我的previewlayer将视频馈送更改为与您点击1x图标将其更改为2x时在相机应用程序中看到的相同比例 设置预览层Ios 如何使用AVCaptureVideoPreviewLayer从照相机应用程序实现2倍变焦,ios,swift,avcapturesession,Ios,Swift,Avcapturesession,我的应用程序中有一个AVCaptureVideoPreviewLayer,运行良好,显示的预览视频与camera应用程序相同。我想实现相机应用程序的2倍变焦功能。我该怎么做 基本上,我希望我的previewlayer将视频馈送更改为与您点击1x图标将其更改为2x时在相机应用程序中看到的相同比例 设置预览层 func startSession(){ captureSession = AVCaptureSession() captureSession?.sessionPreset =
func startSession(){
captureSession = AVCaptureSession()
captureSession?.sessionPreset = AVCaptureSessionPresetPhoto
let backCamera = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)
// Catch error using the do catch block
do {
let input = try AVCaptureDeviceInput(device: backCamera)
if (captureSession?.canAddInput(input) != nil){
captureSession?.addInput(input)
// Setup the preview layer
previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
previewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill
previewLayer?.connection.videoOrientation = AVCaptureVideoOrientation.portrait
tempImageView.layer.addSublayer(previewLayer!)
captureSession?.startRunning()
// Set up AVCaptureVideoDataOutput
let dataOutput = AVCaptureVideoDataOutput()
dataOutput.videoSettings = [(kCVPixelBufferPixelFormatTypeKey as NSString) : NSNumber(value: kCVPixelFormatType_32BGRA as UInt32)]
dataOutput.alwaysDiscardsLateVideoFrames = true
if (captureSession?.canAddOutput(dataOutput) == true) {
captureSession?.addOutput(dataOutput)
}
let queue = DispatchQueue(label: "com.bigbob.videoQueue")
dataOutput.setSampleBufferDelegate(self, queue: queue)
}
} catch _ {
print("Error setting up camera!")
}
使用以下内容设置AVCaptureDevice zoom属性,预览层的缩放也会随之进行
do {
try backCamera?.lockForConfiguration()
let zoomFactor:CGFloat = 2
backCamera?.videoZoomFactor = zoomFactor
backCamera?.unlockForConfiguration()
} catch {
//Catch error from lockForConfiguration
}