Ios 如何调整视频预览以填充图像视图Xcode Swift 3
我正在尝试在我的应用程序中实现一项功能,允许用户拍摄个人资料照片。使用AVFoundation,我可以轻松地将视频预览层输出到全屏。我还尝试将预览层框架设置为imageview框架,但它给出了下图所示的结果Ios 如何调整视频预览以填充图像视图Xcode Swift 3,ios,swift,camera,avfoundation,Ios,Swift,Camera,Avfoundation,我正在尝试在我的应用程序中实现一项功能,允许用户拍摄个人资料照片。使用AVFoundation,我可以轻松地将视频预览层输出到全屏。我还尝试将预览层框架设置为imageview框架,但它给出了下图所示的结果 if let previewLayer = AVCaptureVideoPreviewLayer(session: captureSession){ self.previewLayer = previewLayer //this is for ful
if let previewLayer = AVCaptureVideoPreviewLayer(session: captureSession){
self.previewLayer = previewLayer
//this is for full screen output
//self.view.layer.addSublayer(self.previewLayer)
//self.previewLayer.frame = self.view.layer.frame
//this gives me the result in the photo
cameraOutputImageView.layer.addSublayer(self.previewLayer)
self.previewLayer.frame = cameraOutputImageView.layer.frame
captureSession.startRunning()
print("trying to run capture session")
let dataOutput = AVCaptureVideoDataOutput()
dataOutput.videoSettings = [(kCVPixelBufferPixelFormatTypeKey as NSString):NSNumber(value: kCVPixelFormatType_32BGRA)]
dataOutput.alwaysDiscardsLateVideoFrames = true
if captureSession.canAddOutput(dataOutput){
captureSession.addOutput(dataOutput)
}
captureSession.commitConfiguration()
}
但是,我希望视频提要是一个正方形,它是它要填充的图像视图的大小。下面是它现在的样子:
粉色框是需要用图像填充的。现在它只显示左上角附近的相机输出尝试将预览层设置为imageview边界
self.previewLayer.frame=cameraOutputImageView.layer.bounds
如果使用自动布局约束,则必须确保在加载所有子视图后调用camera函数,因为uiimage的layer.bounds仅在调用ViewDidLayoutSubview后更改