Skip to content

馃敠 Add torch (flashlight) control#7

Description

@superturboryan

Overview

Let's add a way to control the torch on the device.

This could go under a new namespace Control.Flashlight 馃挕

Tactics

Thanks ChatGPT 馃

Check flashlight availability

import AVFoundation

if let device = AVCaptureDevice.default(for: .video), device.hasTorch {
    print("Torch is available")
} else {
    print("Torch is not available")
}

Turn the Flashlight on or off

func toggleFlashlight(on: Bool) {
    guard let device = AVCaptureDevice.default(for: .video), device.hasTorch else {
        print("Torch is not available")
        return
    }
    
    do {
        try device.lockForConfiguration()
        if on {
            try device.setTorchModeOn(level: AVCaptureDevice.maxAvailableTorchLevel)
        } else {
            device.torchMode = .off
        }
        device.unlockForConfiguration()
    } catch {
        print("Error accessing the torch: \(error.localizedDescription)")
    }
}

Testing

  • Unit tests? 馃И馃И

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions