Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/99.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在Swift中打开/关闭iPhone LED(闪光灯)?_Ios_Iphone_Xcode_Swift - Fatal编程技术网

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

Ios 在Swift中打开/关闭iPhone LED(闪光灯)?,ios,iphone,xcode,swift,Ios,Iphone,Xcode,Swift,我可以使用什么代码用一个按钮打开/关闭iPhone flash(Swift) 我是否使用插座或动作进行连接 我将感谢所有的帮助 Xcode 9测试版•Swift 4 import AVFoundation extension AVCaptureDevice { var isLocked: Bool { do { try lockForConfiguration() return true } catch {

我可以使用什么代码用一个按钮打开/关闭iPhone flash(Swift)

我是否使用插座或动作进行连接


我将感谢所有的帮助

Xcode 9测试版•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.
    }
}