简体   繁体   English

如何在Swift中创建一个更改摄像机视图的按钮?

[英]How to create a button to change the camera view in Swift?

I would like to know how I could change the camera view when I press a button. 我想知道当我按下按钮时如何更改相机视图。 At the moment, I am using a boolean var camera = false and when I press a button, I want to change the value to true and get the other camera. 目前,我使用布尔值var camera = false ,当我按下按钮时,我想将值更改为true并获取另一个相机。 But that is not working. 但这不起作用。 I now have this: 我现在有这个:

 @IBAction func changeCamera(sender: AnyObject) {

    camera = true

}

override func viewWillAppear(animated: Bool) {

    captureSession = AVCaptureSession()
    captureSession!.sessionPreset = AVCaptureSessionPresetPhoto
    var captureDevice:AVCaptureDevice! = nil
    //var 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 {
        var captureDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)

    }

    var error: NSError?
    var input = AVCaptureDeviceInput(device: captureDevice, error: &error)

    if error == nil && 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
            previewView.layer.addSublayer(previewLayer)

            captureSession!.startRunning()
        }
    }

}

The problem lies where you are setting the camera source. 问题出在您设置相机源的位置。

You are setting it in viewDidAppear which will only be called when the view appears on the device. 您在viewDidAppear中进行设置,只有在视图出现在设备上时才会调用它。 This is whenever you navigate to that view controller from another or close a presented view controller that is being presented by this one. 这是每当您从另一个视图控制器导航到该视图控制器或关闭由此视图控制器呈现的呈现视图控制器时。

My suggestion would be to move the camera selection code into its own function that is called by both viewDidLoad and also whenever the changeCamera action is called. 我的建议是将摄像机选择代码移动到它自己的函数中,该函数由viewDidLoad和调用changeCamera动作时调用。

@IBAction func changeCamera(sender: AnObject?) {
    camera = !camera

    reloadCamera()
}


func viewDidAppear(animated: Bool) {
    // normal code

    reloadCamera()
}

func reloadCamera() {
    // camera loading code
    captureSession = AVCaptureSession()
    captureSession!.sessionPreset = AVCaptureSessionPresetPhoto
    var captureDevice:AVCaptureDevice! = nil
    // var 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 {
        var captureDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
    }

    var error: NSError?
    var input = AVCaptureDeviceInput(device: captureDevice, error: &error)

    if error == nil && 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
        previewView.layer.addSublayer(previewLayer)

        captureSession!.startRunning()
    }
}

Also, an additional improvement would be to use a custom enum to store which camera is currently in use rather than a Boolean. 此外,另一项改进是使用自定义枚举来存储当前正在使用的相机而不是布尔值。 This means you can add to it later say if there was ever a third camera. 这意味着你可以在以后添加它,如果有第三个相机。 This would look like: 这看起来像:

enum CameraType {
    case front
    case back
}

var camera = CameraType.back

I hope this helps, apologies for omitting the full code examples, currently on an iPad but I'll update when I get to a computer. 我希望这有帮助,对于省略完整的代码示例表示歉意,目前在iPad上,但是当我到达计算机时我会更新。


Update 更新

Make sure you remove the previous preview layer from the view before you change the camera. 在更换相机之前,请确保从视图中删除上一个预览图层。

func reloadCamera() {
    captureSession?.stopRunning()
    previewLayer?.removeFromSuperlayer()

    // The rest of the camera loading code...

This should fix your camera freezing issue. 这应该可以解决您的相机冻结问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Swift - 如何在相机视图中添加圆形视图? - Swift - How to add a circular view into camera view? 如何更改Swift中表视图单元格中的默认删除按钮? - How to change the default delete button in a table view cell in Swift? 如何通过单击swift(Xcode)中子视图内的按钮来更改父视图 - How to change a parent view by clicking a button inside of subview in swift (Xcode) (快速)如何通过触摸按钮在pageviewcontroller中更改视图 - (Swift) how can I change view in pageviewcontroller with touching button 如何快速在 ui 表视图单元格上创建自定义单选按钮 - how to create custom radio button on ui table view cell in swift 如何快速在集合视图中创建动态按钮动作? - How to create dynamic button action in collection view in swift? 如何通过快速点击按钮创建新视图(或子视图)? - How do I create a new View (or subView) with the tap of a button in swift? 如何在 swift5 中自定义相机视图? - how to customize camera view in swift5? 自定义相机视图-无法更改cameraLayout的帧大小UIImagePicker,Swift - Custom Camera View - Cannot change the frame size of the cameraLayout UIImagePicker, Swift Swift:在点击时在表格视图中更改按钮图像 - Swift: Change button image in table view on click
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM