自定义相机宽高比

时间:2015-08-25 14:48:24

标签: ios swift

我正在使用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)

但是当我想要显示预览图像时,相机可以正常工作:

相机:

enter image description here

拍摄预览后:

enter image description here

它应该看起来像相机视图。我该如何解决?

1 个答案:

答案 0 :(得分:0)

如果您想制作宽高比3:4,则应设置:

captureSession.sessionPreset = .photo