11

我可以用一个按钮打开手电筒,用另一个按钮将其关闭。但我只想用一个按钮来完成。但是,我没有允许我使用 bool isSelected 方法的框架。所以我对如何将两个功能合并到一个按钮中一无所知。

这是有效的代码:

-(void)onButtonPressed 
{

AVCaptureDevice *flashLight = [AVCaptureDevice 
defaultDeviceWithMediaType:AVMediaTypeVideo];
if([flashLight isTorchAvailable] && [flashLight
isTorchModeSupported:AVCaptureTorchModeOn])
{
    BOOL success = [flashLight lockForConfiguration:nil];
    if(success){
        [flashLight setTorchMode:AVCaptureTorchModeOn];
        [flashLight unlockForConfiguration];
    }
}

}

我用它来关闭手电筒。

-(void)offButtonPressed {

AVCaptureDevice *flashLight = [AVCaptureDevice
defaultDeviceWithMediaType:AVMediaTypeVideo];
if([flashLight isTorchAvailable] && [flashLight
isTorchModeSupported:AVCaptureTorchModeOn])
{
    BOOL success = [flashLight lockForConfiguration:nil];
    if(success){
        [flashLight setTorchMode:AVCaptureTorchModeOff];
        [flashLight unlockForConfiguration];
    }
}


}

我并不特别关注它的完成方式。只要手电筒在第一次轻敲时打开并在第二次轻敲时关闭,我就不会关心这种方法。

但是,我正在使用以编程方式制作的 barbuttonitems,所以请不要给我 IBAction 方法。如果建议的方法尽可能简单,我也会很感激,我认为我现在使用手电筒的方式过于复杂。

4

7 回答 7

30

我刚刚在我的应用程序上实现了这个功能。回答您的问题,这里是如何以一种方法合并这两个功能。

- (void) flashlight
{
    AVCaptureDevice *flashLight = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    if ([flashLight isTorchAvailable] && [flashLight isTorchModeSupported:AVCaptureTorchModeOn])
    {
        BOOL success = [flashLight lockForConfiguration:nil];
        if (success) 
        {
            if ([flashLight isTorchActive]) {
                [flashLight setTorchMode:AVCaptureTorchModeOff];
            } else {
                [flashLight setTorchMode:AVCaptureTorchModeOn];
            }
            [flashLight unlockForConfiguration];
        }
    }
}
于 2013-02-06T22:57:36.697 回答
6

对于迅捷3

@IBAction 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")
    }
}

}

于 2016-12-07T00:02:27.520 回答
1

问题陈述 :


使用单个按钮打开和关闭移动设备(支持 iOS)上的手电筒。

解决方案

  1. 在视图控制器上创建一个按钮。
  2. 设置按钮操作并将目标添加为自身。

注意:在执行前在界面生成器中将标签设置为 101,这会将标签值初始化为 101。

下面是应该在ViewController.m文件中的 action 方法中编写的代码


- (IBAction)flashLightButtonTapped:(id)sender
{

    UIButton *button = sender;
    AVCaptureDevice *flashLight = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

    if (button.tag==101)
    {

        if ([flashLight isTorchAvailable] && [flashLight isTorchModeSupported:AVCaptureTorchModeOn])
        {
            BOOL success = [flashLight lockForConfiguration:nil];
            if (success)
            {
                if ([flashLight isTorchActive])
                {
                    [flashLight setTorchMode:AVCaptureTorchModeOff];
                }
                else
                {
                    [flashLight setTorchMode:AVCaptureTorchModeOn];
                }
                [flashLight unlockForConfiguration];
            }
        }
        button.tag=102;
    }
    else if (button.tag==102)
    {
        if ([flashLight isTorchAvailable] && [flashLight isTorchModeSupported:AVCaptureTorchModeOn])
        {
            BOOL success = [flashLight lockForConfiguration:nil];
            if (success)
            {
                if ([flashLight isTorchActive])
                {
                    [flashLight setTorchMode:AVCaptureTorchModeOn];
                }
                else
                {
                    [flashLight setTorchMode:AVCaptureTorchModeOff];

                }
                [flashLight unlockForConfiguration];
            }
        }
        button.tag=101;
    }

}
于 2015-07-26T09:17:59.163 回答
0
- (IBAction)flash:(UIButton *)sender;
{
    Class captureDeviceClass = NSClassFromString(@"AVCaptureDevice");
    if (captureDeviceClass != nil) {
        AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
        if ([device hasTorch] && [device hasFlash]){

            [device lockForConfiguration:nil];
            if (device.torchMode == AVCaptureTorchModeOff) {
                [device setTorchMode:AVCaptureTorchModeOn];
                [device setFlashMode:AVCaptureFlashModeOn];
                //TorchIsOn = YES;

                [self.flashton setHighlighted:YES];
            } else {
                [device setTorchMode:AVCaptureTorchModeOff];
                [device setFlashMode:AVCaptureFlashModeOff];
                //TorchIsOn = NO;

                [self.flashton setHighlighted:NO];
            }
            [device unlockForConfiguration];
        }
    }
}
于 2013-10-19T01:16:55.853 回答
0

在这里关闭评论,这就是我的想法:

.h

@property (nonatomic) int flashlightState;

.m

-(void) viewDidLoad:
{
    //Any previous code that you may have
    flashlightState = 0;
}
-(void) flashlightStateControl:
{

    if (flashlightState == 1)
    {
        offButtonPress();
        flashlightState = 0;
    }
    else
    {
        onButtonPress();
        flashlightState = 1;
    }
}

这个想法是,你现在调用flashlightStateControl(). 那应该做你想做的,非常欢迎你调整它的一部分(你可能需要),但这是代码形式的一般想法。希望对你有用!

于 2012-07-30T18:05:03.387 回答
0

斯威夫特 5 版本

@IBAction func toggleTorch() {
    guard let device = AVCaptureDevice.default(for: .video), device.hasTorch else {
        showTorchNotSupported()
        return
    }
    
    do {
        try device.lockForConfiguration()
        let torchOn = !device.isTorchActive
        try device.setTorchModeOn(level: 1.0)
        device.torchMode = torchOn ? .on : .off
        device.unlockForConfiguration()
    } catch {
        showTorchNotSupported()
    }
}

private func showTorchNotSupported() {
    let alertController = UIAlertController(title: "Flashlight is not supported", message: nil, preferredStyle: .alert)
    alertController.addAction(UIAlertAction(title: "Understand", style: .default, handler: nil))
    present(alertController, animated: true)
}
于 2020-07-08T11:10:30.887 回答
0

基于 Chuy47 回答的 Swift 5 解决方案:

import Foundation
import AVFoundation

extension AVCaptureDevice {

    /// toggles the device's flashlight, if possible
    static func toggleFlashlight() {
        guard let device = AVCaptureDevice.default(for: AVMediaType.video), device.hasTorch else { return }
        do {
            try device.lockForConfiguration()
            let torchOn = !device.isTorchActive
            try device.setTorchModeOn(level: 1.0)
            device.torchMode = torchOn ? .on : .off
            device.unlockForConfiguration()
        } catch {
            print("Error toggling Flashlight: \(error)")
        }
    }
}
于 2019-07-17T14:19:41.500 回答