在Swift中打开/关闭iPhone LED(闪光灯)? [英] Turn the iPhone LED (flash) on/off in Swift?

查看:292
本文介绍了在Swift中打开/关闭iPhone LED(闪光灯)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

What code can I use to turn the iPhone flash on/off (in Swift) using one button.

我可以使用插座或操作进行连接吗?

Do I connect using a Outlet or Action?

感谢您的帮助!

推荐答案

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


用法:


usage:

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

这篇关于在Swift中打开/关闭iPhone LED(闪光灯)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆