简体   繁体   中英

Custom camera aspect ratio

I am creating a custom camera view with AVFoundation with following code:

    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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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