如何检测前置摄像头是否处于活动状态

时间:2016-02-22 12:47:30

标签: ios swift

我有一个uibutton来激活和停用闪光灯。当我将相机改为前置时,我需要隐藏按钮,因为前面没有闪光灯。而且,当我再次将凸轮更换回来时需要取消隐藏。感谢您的帮助。

 @IBAction func changeCamera(sender: AnyObject) {

    dispatch_async(self.sessionQueue, {

        let currentVideoDevice:AVCaptureDevice = self.videoDeviceInput!.device
        let currentPosition: AVCaptureDevicePosition = currentVideoDevice.position
        var preferredPosition: AVCaptureDevicePosition = AVCaptureDevicePosition.Unspecified

        switch currentPosition{
        case AVCaptureDevicePosition.Front:
            preferredPosition = AVCaptureDevicePosition.Back

        case AVCaptureDevicePosition.Back:
            preferredPosition = AVCaptureDevicePosition.Front

        case AVCaptureDevicePosition.Unspecified:
            preferredPosition = AVCaptureDevicePosition.Back

        }


        let device:AVCaptureDevice = takePhotoScreen.deviceWithMediaType(AVMediaTypeVideo, preferringPosition: preferredPosition)

        var videoDeviceInput: AVCaptureDeviceInput?

        do {
            videoDeviceInput = try AVCaptureDeviceInput(device: device)
        } catch _ as NSError {
            videoDeviceInput = nil
        } catch {
            fatalError()
        }


        self.session!.beginConfiguration()

        self.session!.removeInput(self.videoDeviceInput)

        if self.session!.canAddInput(videoDeviceInput){

            NSNotificationCenter.defaultCenter().removeObserver(self, name:AVCaptureDeviceSubjectAreaDidChangeNotification, object:currentVideoDevice)

            takePhotoScreen.setFlashMode(AVCaptureFlashMode.Auto, device: device)

            NSNotificationCenter.defaultCenter().addObserver(self, selector: "subjectAreaDidChange:", name: AVCaptureDeviceSubjectAreaDidChangeNotification, object: device)

            self.session!.addInput(videoDeviceInput)
            self.videoDeviceInput = videoDeviceInput

        }else{
            self.session!.addInput(self.videoDeviceInput)
        }

        self.session!.commitConfiguration()



        dispatch_async(dispatch_get_main_queue(), {

            self.snapButton.enabled = true
            self.cameraButton.enabled = true

        })

    })

}


@IBAction func toggleTorch(sender: AnyObject) {
    let device = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
    if (device.hasTorch) {
        do {
            try device.lockForConfiguration()
            if (device.torchMode == AVCaptureTorchMode.On) {
                device.torchMode = AVCaptureTorchMode.Off
            } else {
                do {
                    try device.setTorchModeOnWithLevel(1.0)
                } catch {
                    print(error)
                }
            }
            device.unlockForConfiguration()
        } catch {
            print(error)
        }
    }    }

1 个答案:

答案 0 :(得分:0)

你可以在这里写隐藏的隐藏代码

 switch currentPosition{
    case AVCaptureDevicePosition.Front:{
           preferredPosition = AVCaptureDevicePosition.Back;
         // UNHIDE FLASH BUTTON HERE
           break;
         }

    case AVCaptureDevicePosition.Back:{
            preferredPosition = AVCaptureDevicePosition.Front
         // HIDE FLASH BUTTON HERE
           break;
         }

    case AVCaptureDevicePosition.Unspecified:
        preferredPosition = AVCaptureDevicePosition.Back

    }

P.S - 我不太了解快速语法,但这应该指向正确的方向。使用开关时也可以使用break。不知道它是否需要swift 2.0,但仍然是它的良好实践。