简体   繁体   English

自定义相机纵横比

[英]Custom camera aspect ratio

I am creating a custom camera view with AVFoundation with following code: 我正在使用以下代码通过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

And it works well. 而且效果很好。 Then I am capturing and showing image on the screen with following code: 然后,我使用以下代码捕获并在屏幕上显示图像:

    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)

But camera works properly when i want to show preview image it looks like this: 但是当我想显示预览图像时,相机可以正常工作:

Camera: 相机:

在此处输入图片说明

After the shot preview: 镜头预览后:

在此处输入图片说明

It should look like the camera view. 它看起来应该像相机视图。 How can I fix it? 我该如何解决?

如果要使纵横比为3:4 ,则应设置:

captureSession.sessionPreset = .photo

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM