如何开启/关闭iPhone相机闪光灯?

时间:2015-11-07 17:02:00

标签: ios iphone flash swift2

我正在寻找如何打开/关闭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()
    }
}

我确实得到了2个问题,一个在线:

avDevice.lockForConfiguration(nil)

和另一个在线:

avDevice.setTorchModeOnWithLevel(1.0, error:nil)

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

6 个答案:

答案 0 :(得分:11)

@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()
    }
}

答案 1 :(得分:1)

出于某种原因" avDevice.torchActive"永远是假的,即使火炬打开,也无法关闭,但我通过声明一个最初设置为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()
}

}

答案 2 :(得分:1)

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)
            }
        }

    }

检查flashmode状态。

// 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)
                }

答案 3 :(得分:0)

Swift 4版本,改编自Ivan Slavov的回答。如果您想花哨的话,也可以选择“ TorchMode.auto”。

    @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()
    }
}

答案 4 :(得分:0)

另一种简便的方法是

application/config/config.php

答案 5 :(得分:0)

Swift 5.4 & Xcode 12.4 & iOS 14.4.2

val modifierA = Modifier.size(100.dp)
val modifierB = Modifier.background(Color.Red)