How to turn flashlight ON and OFF in swift?

前端 未结 11 1449
青春惊慌失措
青春惊慌失措 2020-12-02 15:42

I\'d like to add flashlight functionality to my app in Swift. How can I go about doing that?

相关标签:
11条回答
  • 2020-12-02 16:08

    For swift 3

    func toggleFlash() {
        if let device = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo), device.hasTorch {
            do {
                try device.lockForConfiguration()
                let torchOn = !device.isTorchActive
                try device.setTorchModeOnWithLevel(1.0)
                device.torchMode = torchOn ? .on : .off
                device.unlockForConfiguration()
            } catch {
                print("error")
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-02 16:09

    For xcode 9.1, swift 4 (updated to not crash if no torch):

       func toggleFlash() {
        let device = AVCaptureDevice.default(for: AVMediaType.video)
    
        if (device != nil) {
            if (device!.hasTorch) {
                do {
                    try device!.lockForConfiguration()
                        if (device!.torchMode == AVCaptureDevice.TorchMode.on) {
                            device!.torchMode = AVCaptureDevice.TorchMode.off
                        } else {
                            do {
                                try device!.setTorchModeOn(level: 1.0)
                                } catch {
                                    print(error)
                                }
                        }
    
                        device!.unlockForConfiguration()
                } catch {
                    print(error)
                }
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-02 16:09

    Solution For Swift 4 With Condition torch is available or not

     func flashlight() {
                guard let device = AVCaptureDevice.default(for: AVMediaType.video) else{
                    return
                }
                if (device.hasTorch) {
                        do {
                            try device.lockForConfiguration()
                            if (device.torchMode == .on) {
                                device.torchMode = .off
                            } else {
                                device.torchMode = .on
    
                            }
                            device.unlockForConfiguration()
                        } catch {
    
                            print("Torch could not be used")
                            print(error)
                        }
                    }
                else{
                    print("Torch is not available")
                }
            }
    

    The Solution is Combination of @Joshua Dance And @Lance

    0 讨论(0)
  • 2020-12-02 16:10

    I've updated @Lyndsey Scott's great answer for Swift 2.0

    let device = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
        if (device.hasTorch) {
            do {
                try device.lockForConfiguration()
                if (device.torchMode == AVCaptureTorchMode.On) {
                    device.torchMode = AVCaptureTorchMode.Off
                } else {
                    try device.setTorchModeOnWithLevel(1.0)
                }
                device.unlockForConfiguration()
            } catch {
                print(error)
            }
        }
    
    0 讨论(0)
  • 2020-12-02 16:12

    Updated Swift 4 Answer:

    func toggleTorch(on: Bool) {
        guard 
            let device = AVCaptureDevice.default(for: AVMediaType.video),
            device.hasTorch
        else { return }
    
        do {
            try device.lockForConfiguration()
            device.torchMode = on ? .on : .off                    
            device.unlockForConfiguration()
        } catch {
            print("Torch could not be used")
        }
    }
    

    Then to actually turn it on or off, call the function and pass in a true or false boolean.

    toggleTorch(on: true) of toggleTorch(on: false)

    I got this answer from Hacking with Swift, however their example had an error in it.

    They used AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo) but this produces an error saying defaultDevice doesn't exist. So I changed it to AVCaptureDevice.default(for: AVMediaType.video)

    0 讨论(0)
提交回复
热议问题