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

问题描述 投票:2回答:4

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

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

ios iphone flash swift2
4个回答
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
投票

由于某种原因,“avDevice.torchActive”总是假的,即使火炬打开,也无法关闭,但我通过声明一个最初设置为false的布尔值来修复它,每次闪光灯打开时,布尔值设置为真正。

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

}


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

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()
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.