我正在用AVFoundation创建一个自定义摄像机视图,代码如下:
captureSession.addInput(AVCaptureDeviceInput(device: captureDevice, error: &err))
captureSession.addOutput(stillOutput)
if err != nil {
println("error: \(err?.localizedDescription)")
}
previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
previewLayer?.videoGravity=AVLayerVideoGravityResizeAspectFill而且效果很好。然后,我用以下代码在屏幕上捕获和显示图像:
self.stillOutput.captureStillImageAsynchronouslyFromConnection(videoConnection){
(imageSampleBuffer : CMSampleBuffer!, _) in
let imageDataJpeg = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(imageSampleBuffer)
var pickedImage: UIImage = UIImage(data: imageDataJpeg)!
var previewImageView=UIImageView()
self.previewImageView.frame = CGRect(x:0, y:0, width:UIScreen.mainScreen().bounds.width, height:UIScreen.mainScreen().bounds.height)
self.previewImageView.layer.zPosition = -1
self.view.addSubview(self.previewImageView)但是,当我想要显示预览图像时,相机工作正常--它看起来如下:
相机:

镜头预览后的:

它应该看起来像摄像机的视角。我怎么才能修好它?
发布于 2018-04-23 19:19:02
如果您想要使高宽比3:4,您应该设置:
captureSession.sessionPreset = .photo
https://stackoverflow.com/questions/32207212
复制相似问题