gpt4 book ai didi

ios - 如何检测前置摄像头是否处于事件状态

转载 作者:行者123 更新时间:2023-11-30 13:41:06 24 4
gpt4 key购买 nike

我有一个 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)
}
} }

最佳答案

您可以在这里编写隐藏取消隐藏代码

 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 - 我不太了解 swift 语法,但这应该会为您指明正确的方向。每当使用 switch 时也使用break。不知道 swift 2.0 是否需要它,但仍然是一个很好的实践。

关于ios - 如何检测前置摄像头是否处于事件状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35554114/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com