相机预览不适合UIView

时间:2015-10-31 01:07:13

标签: ios iphone swift uiview avcapturesession

我正在制作相机,我决定将它变成像Instagram相机和iPhone默认相机那样的方形。我遇到的问题是图像不适合UIView。以下是它发生的一个例子。 UIView应该覆盖左右两边的红色边缘。

以下是启动会话的代码......

func reloadCamera() {

    cameraView.backgroundColor = UIColor.clearColor()

    captureSession = AVCaptureSession()

    captureSession!.sessionPreset = AVCaptureSessionPresetHigh

    let backCamera = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
    if (camera == false) {
        let videoDevices = AVCaptureDevice.devicesWithMediaType(AVMediaTypeVideo)

        for device in videoDevices {
            let device = device as! AVCaptureDevice
            if device.position == AVCaptureDevicePosition.Front {
                captureDevice = device
                break

            } else {
                captureDevice = backCamera
            }
        }

    } else {
        captureDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
    }

    do {
        let input = try AVCaptureDeviceInput(device: captureDevice)
        if captureSession!.canAddInput(input) {
            captureSession!.addInput(input)

            stillImageOutput = AVCaptureStillImageOutput()
            stillImageOutput!.outputSettings = [AVVideoCodecKey: AVVideoCodecJPEG]
            if captureSession!.canAddOutput(stillImageOutput) {
                captureSession!.addOutput(stillImageOutput)

                previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
                previewLayer!.videoGravity = AVLayerVideoGravityResizeAspect
                previewLayer!.connection?.videoOrientation = AVCaptureVideoOrientation.Portrait
                cameraView.layer.addSublayer(previewLayer!)
                previewLayer?.frame = cameraView.bounds

                captureSession!.startRunning()
            }
        }
    } catch let error as NSError {
        // Handle any errors
        print(error)
    }

}

没有任何约束问题我已经尝试删除所有元素并且只运行上面的代码但是它不起作用。如果您需要更多信息,请告诉我。谢谢! (:

0 个答案:

没有答案