swift2 如何打开/关闭iPhone相机闪光灯swift 2?

sauutmhj  于 2022-11-06  发布在  Swift
关注(0)|答案(6)|浏览(193)

我正在寻找如何打开/关闭iPhone的相机闪光灯,我发现了这个:

@IBAction func didTouchFlashButton(sender: AnyObject) {
    let avDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)

// check if the device has torch
if avDevice.hasTorch {
    // lock your device for configuration
    avDevice.lockForConfiguration(nil)
    // check if your torchMode is on or off. If on turns it off otherwise turns it on
    if avDevice.torchActive {
        avDevice.torchMode = AVCaptureTorchMode.Off
    } else {
        // sets the torch intensity to 100%
        avDevice.setTorchModeOnWithLevel(1.0, error: nil)
    }
    // unlock your device
    avDevice.unlockForConfiguration()
    }
}

我有两个问题,一个在线:

avDevice.lockForConfiguration(nil)

另一个在线:

avDevice.setTorchModeOnWithLevel(1.0, error:nil)

这两个问题都与异常处理有关,但我不知道如何解决它们。

sqyvllje

sqyvllje1#

@IBAction func didTouchFlashButton(sender: UIButton) {
    let avDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)

    // check if the device has torch
    if avDevice.hasTorch {
        // lock your device for configuration
        do {
            let abv = try avDevice.lockForConfiguration()
        } catch {
            print("aaaa")
        }

        // check if your torchMode is on or off. If on turns it off otherwise turns it on
        if avDevice.torchActive {
            avDevice.torchMode = AVCaptureTorchMode.Off
        } else {
            // sets the torch intensity to 100%
            do {
                let abv = try avDevice.setTorchModeOnWithLevel(1.0)
            } catch {
                print("bbb")
            }
        //    avDevice.setTorchModeOnWithLevel(1.0, error: nil)
        }
        // unlock your device
        avDevice.unlockForConfiguration()
    }
}
vuktfyat

vuktfyat2#

Swift 4版本,改编自Ivan Slavov的答案。TorchMode.auto如果你想变得花哨,“www.example.com“也是一个选项。

@IBAction func didTouchFlashButton(_ sender: Any) {
    if let avDevice = AVCaptureDevice.default(for: AVMediaType.video) {
        if (avDevice.hasTorch) {
            do {
                try avDevice.lockForConfiguration()
            } catch {
                print("aaaa")
            }

            if avDevice.isTorchActive {
                avDevice.torchMode = AVCaptureDevice.TorchMode.off
            } else {
                avDevice.torchMode = AVCaptureDevice.TorchMode.on
            }
        }
        // unlock your device
        avDevice.unlockForConfiguration()
    }
}
f3temu5u

f3temu5u3#

Swift 5.4和Xcode 12.4以及iOS 14.4.2

@objc private func flashEnableButtonAction() {

        guard let captureDevice = AVCaptureDevice.default(for: AVMediaType.video) else {
            return
        }

        if captureDevice.hasTorch {
            do {
                let _: () = try captureDevice.lockForConfiguration()
            } catch {
                print("aaaa")
            }

            if captureDevice.isTorchActive {
                captureDevice.torchMode = AVCaptureDevice.TorchMode.off
            } else {

                do {
                    let _ = try captureDevice.setTorchModeOn(level: 1.0)
                } catch {
                    print("bbb")
                }
            }

            captureDevice.unlockForConfiguration()
        }

    }
ghhkc1vu

ghhkc1vu4#

由于某些原因,“avDevice.torchActive”总是为false,即使在闪光灯打开时也是如此,这使得它不可能关闭,但我通过声明一个布尔值来修复它,该布尔值最初设置为false,每次闪光灯打开时,该布尔值都设置为true。

var on: Bool = false    

@IBAction func didTouchFlashButton(sender: UIButton) {
let avDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)

// check if the device has torch
if avDevice.hasTorch {
    // lock your device for configuration
    do {
        let abv = try avDevice.lockForConfiguration()
    } catch {
        print("aaaa")
    }

    // check if your torchMode is on or off. If on turns it off otherwise turns it on
    if on == true {
        avDevice.torchMode = AVCaptureTorchMode.Off
        on = false
    } else {
        // sets the torch intensity to 100%
        do {
            let abv = try avDevice.setTorchModeOnWithLevel(1.0)
            on = true
        } catch {
            print("bbb")
        }
    //    avDevice.setTorchModeOnWithLevel(1.0, error: nil)
    }
    // unlock your device
    avDevice.unlockForConfiguration()
}

}

ylamdve6

ylamdve65#

import AVFoundation

var videoDeviceInput: AVCaptureDeviceInput?
var movieFileOutput: AVCaptureMovieFileOutput?
var stillImageOutput: AVCaptureStillImageOutput?

将类方法添加到ViewController。

class func setFlashMode(flashMode: AVCaptureFlashMode, device: AVCaptureDevice){

        if device.hasFlash && device.isFlashModeSupported(flashMode) {
            var error: NSError? = nil
            do {
                try device.lockForConfiguration()
                device.flashMode = flashMode
                device.unlockForConfiguration()
            } catch let error1 as NSError {
                error = error1
                print(error)
            }
        }

    }

检查闪光灯模式状态。

// Flash set to Auto/Off for Still Capture
                print("flashMode.rawValue : \(self.videoDeviceInput!.device.flashMode.rawValue)")
                if(self.videoDeviceInput!.device.flashMode.rawValue == 1)
                {
                    CameraViewController.setFlashMode(AVCaptureFlashMode.On, device: self.videoDeviceInput!.device)
                }
                else if (self.videoDeviceInput!.device.flashMode.rawValue == 2)
                {
                    CameraViewController.setFlashMode(AVCaptureFlashMode.Auto, device: self.videoDeviceInput!.device)
                }
                else
                {
                    CameraViewController.setFlashMode(AVCaptureFlashMode.Off, device: self.videoDeviceInput!.device)
                }
ecbunoof

ecbunoof6#

另一个捷径是这样做

let devices = AVCaptureDevice.devices()
        let device = devices[0]
               guard device.isTorchAvailable else { return }
               do {
                 try device.lockForConfiguration()
                 if device.torchMode == .on {
                    device.torchMode = .off
                 }else{
                   device.torchMode = .on
                   }
               } catch {
                 debugPrint(error)
    }

相关问题