前置摄像头填充圆形UIView [英] Front Camera to Fill Circular UIView

查看:183
本文介绍了前置摄像头填充圆形UIView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In an app I'm developing the user is required to take a "selfie" (Yes, I know, but the app is for private use only).

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

I've got everything working with the camera showing in the circular UIView region, however I cannot get it to scale and fill the circle properly. Here's what it's doing now:

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

And here's what I want it to be doing:

这是我的UIView的代码:

Here's the code for my 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。它们都会产生完全相同的结果。

I have tried a few different contentMode options, include ScaleToFill, ScaleAspectFill, and ScaleAspectFit. They all generate the same exact result.

推荐答案

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

As it turns out, the camera's "self.previewLayer" has a property that determines how the camera's content fills a View.

在以下代码中,我将self.previewLayer.videoGravity = AVLayerVideoGravityResizeAspect更改为self.previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill

In the following code I changed "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()
    }

}

这篇关于前置摄像头填充圆形UIView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆