在Swift中打开/关闭iPhone LED(闪光灯)?

时间:2015-03-11 23:43:11

标签: ios iphone xcode swift

使用一个按钮可以使用什么代码打开/关闭iPhone闪光灯(在Swift中)。

我是否使用Outlet或Action进行连接?

我很感激所有的帮助!

1 个答案:

答案 0 :(得分:8)

Xcode 9 beta•Swift 4

import AVFoundation
extension AVCaptureDevice {
    var isLocked: Bool {
        do {
            try lockForConfiguration()
            return true
        } catch {
            print(error)
            return false
        }
    }
    func setTorch(intensity: Float) {
       guard hasTorch && isLocked else { return }
        defer { unlockForConfiguration() }
        if intensity > 0 {
            if torchMode == .off {
                torchMode = .on
            }
            do {
                try setTorchModeOn(level: intensity)
            } catch {
                print(error)
            }
        } else {
            torchMode = .off
        }
    }
}

用法:

import UIKit
import AVFoundation

class ViewController: UIViewController {
    var device: AVCaptureDevice!
    override func viewDidLoad() {
        super.viewDidLoad()
         device = AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, position: .unspecified)
    }
    @IBAction func slider(_ sender: UISlider) {
        device.setTorch(intensity: sender.value)
    }
}

Xcode 8.2.1•Swift 3.0.2

extension AVCaptureDevice {
    @discardableResult
    class func toggleTorch() -> Bool {
        guard let defaultDevice = AVCaptureDevice.defaultDevice(withDeviceType: .builtInWideAngleCamera, mediaType: AVMediaTypeVideo, position: .unspecified)
        else {
            print("early exit")
            return false
        }
        // Check if the default device has torch
        if  defaultDevice.hasTorch {
            print("defaultDevice.hasTorch:", defaultDevice.hasTorch)

            // Lock your default device for configuration
            do {
                // unlock your device when done
                defer {
                    defaultDevice.unlockForConfiguration()
                    print("defaultDevice.unlockedForConfiguration:", true)
                }
                try defaultDevice.lockForConfiguration()
                print("defaultDevice.lockedForConfiguration:", true)
                // Toggles the torchMode
                defaultDevice.torchMode = defaultDevice.torchMode == .on ? .off : .on
                // Sets the torch intensity to 100% if torchMode is ON
                if defaultDevice.torchMode == .on {
                    do {
                        try defaultDevice.setTorchModeOnWithLevel(1)
                    } catch {
                        print(error.localizedDescription)
                    }
                }
            } catch {
                print(error.localizedDescription)
            }
        }
        print("defaultDevice.torchMode:", defaultDevice.torchMode == .on)
        return defaultDevice.torchMode == .on
    }
}

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(tap(_:))))
    }
    func tap(_ gesture: UITapGestureRecognizer) {
        if AVCaptureDevice.toggleTorch() {
            print("TORCH IS ON")
        } else {
            print("TORCH IS OFF")
        }
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}