前置摄像头填充圆形UIView

时间:2015-02-23 21:32:53

标签: ios swift uiview

在我正在开发的应用程序中,用户需要进行“自拍”(是的,我知道,但该应用程序仅供私人使用)。

我已经完成了在圆形UIView区域中显示的相机的一切,但是我无法将其缩放并正确填充圆圈。以下是它现在正在做的事情:

What it's doing

这就是我希望它做的事情:

What I want

这是我的UIView的代码:

var cameraView = UIView()
cameraView.frame = CGRectMake(100, self.view.center.y-260, 568, 568)
cameraView.backgroundColor = UIColor(red:26/255, green:188/255, blue:156/255, alpha:1)
cameraView.layer.cornerRadius = 284
cameraView.layer.borderColor = UIColor.whiteColor().CGColor
cameraView.layer.borderWidth = 15
cameraView.contentMode = UIViewContentMode.ScaleToFill
cameraView.layer.masksToBounds = true

我尝试了一些不同的contentMode选项,包括ScaleToFill,ScaleAspectFill和ScaleAspectFit。它们都会产生完全相同的结果。

1 个答案:

答案 0 :(得分:1)

事实证明,相机" self.previewLayer"有一个属性,用于确定相机的内容如何填充视图。

在下面的代码中,我更改了" self.previewLayer.videoGravity = AVLayerVideoGravityResizeAspect" to" self.previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill"

extension SelfieViewController:  AVCaptureVideoDataOutputSampleBufferDelegate{
    func setupAVCapture(){
        session.sessionPreset = AVCaptureSessionPreset640x480

        let devices = AVCaptureDevice.devices();
        // Loop through all the capture devices on this phone
        for device in devices {
            // Make sure this particular device supports video
            if (device.hasMediaType(AVMediaTypeVideo)) {
                // Finally check the position and confirm we've got the front camera
                if(device.position == AVCaptureDevicePosition.Front) {
                    captureDevice = device as? AVCaptureDevice
                    if captureDevice != nil {
                        beginSession()
                        break
                    }
                }
            }
        }
    }

    func beginSession(){
        var err : NSError? = nil
        var deviceInput:AVCaptureDeviceInput = AVCaptureDeviceInput(device: captureDevice, error: &err)
        if err != nil {
            println("error: \(err?.localizedDescription)")
        }
        if self.session.canAddInput(deviceInput){
            self.session.addInput(deviceInput)
        }

        self.videoDataOutput = AVCaptureVideoDataOutput()
        var rgbOutputSettings = [NSNumber(integer: kCMPixelFormat_32BGRA):kCVPixelBufferPixelFormatTypeKey]
        self.videoDataOutput.alwaysDiscardsLateVideoFrames=true
        self.videoDataOutputQueue = dispatch_queue_create("VideoDataOutputQueue", DISPATCH_QUEUE_SERIAL)
        self.videoDataOutput.setSampleBufferDelegate(self, queue:self.videoDataOutputQueue)
        if session.canAddOutput(self.videoDataOutput){
            session.addOutput(self.videoDataOutput)
        }
        self.videoDataOutput.connectionWithMediaType(AVMediaTypeVideo).enabled = true

        self.previewLayer = AVCaptureVideoPreviewLayer(session: self.session)
        self.previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill

        var rootLayer :CALayer = self.cameraView.layer
        rootLayer.masksToBounds=true
        self.previewLayer.frame = rootLayer.bounds
        rootLayer.addSublayer(self.previewLayer)
        session.startRunning()

    }

    func captureOutput(captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, fromConnection connection: AVCaptureConnection!) {
        // do stuff here
    }

    // clean up AVCapture
    func stopCamera(){
        session.stopRunning()
    }

}