如何在swift上使用方形相机

时间:2016-05-07 15:09:10

标签: ios swift

当我使用ios系统相机时,我可以选择方形相机类型。我想在swift上实现这个,但是我该怎么做呢?以下是我用于相机的代码。 initialSession()用于创建AVCaptureSession对象。 setUpCameraLayer()方法是在视图上显示摄像头。如何在下面的代码上使用方形相机?

func initialSession(){
        self.session = AVCaptureSession()
        do {
            try self.videoInput = AVCaptureDeviceInput(device: self.backCamera())
        } catch {

        }

        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)
        }
    }

   func setUpCameraLayer(){


    if self.previewLayer == nil {
        self.previewLayer = AVCaptureVideoPreviewLayer(session: self.session)
        self.backView.frame = CGRectMake(0, 0, self.view.bounds.width,self.view.bounds.width)
        let view:UIView = self.backView
        let viewLayer:CALayer = view.layer
        viewLayer.masksToBounds = true
        let bounds:CGRect = view.bounds
        self.previewLayer?.frame = bounds
        self.previewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill//AVLayerVideoGravityResizeAspect
        //viewLayer.insertSublayer(self.previewLayer!.below: viewLayer.sublayers![0])
        view.layer.addSublayer(self.previewLayer!)

        self.picture.frame = self.backView.frame
        self.backView.addSubview(self.picture)
        self.picture.hidden = true
    }


}

0 个答案:

没有答案