【发布时间】:2016-05-08 09:02:21
【问题描述】:
我使用以下代码在 iPhone 设备上显示相机。相机可以显示 UI,我可以拍照。但是,相机只显示在 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!)
下面是摄像头的截图。
【问题讨论】: