如何让 iPhone 4 LED 灯瞬间发光?

发布于 2024-09-28 05:39:17 字数 2176 浏览 8 评论 0原文

我目前正在使用下面的代码来打开和关闭我的 iPhone 4 LED 灯,它工作得很好,但唯一的问题是每次我打开 LED 时都会有轻微的延迟。但它会立即关闭。我需要它立即触发来实现类似频闪的功能,因为它更方便。

我注意到,在 Apple 的相机应用程序和许多其他应用程序中,当您按下电源按钮时,LED 会立即打开和关闭。

我尝试将一些对象(如“会话”和“设备”)作为实例变量添加到我的视图控制器中,以便让 iPhone 在加载时创建这些对象,但是我没有运气让它工作。

我也尝试过查看苹果 WWDC 示例代码,但我似乎无法破译他们的复杂代码。有人可以帮我解决这个问题吗?我已经尝试了大约 4 天才能让它发挥作用。

.h.m

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>

@interface FlashlightViewController : UIViewController {

    AVCaptureSession *torchSession;
}

@property (nonatomic, retain) AVCaptureSession * torchSession;

- (void) toggleTorch;

@end

#import "FlashlightViewController.h"

@implementation FlashlightViewController

@synthesize torchSession;

- (void)dealloc 
{
    [torchSession release];
    [super dealloc];
}

- (void)viewDidLoad 
{
    [self toggleTorch];
    [super viewDidLoad];
}

- (void) toggleTorch 
{
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

    if ([device hasTorch] && [device hasFlash])
    {
        if (device.torchMode == AVCaptureTorchModeOff) 
        {
            NSLog(@"It's currently off.. turning on now.");

            AVCaptureDeviceInput *flashInput = [AVCaptureDeviceInput deviceInputWithDevice:device error: nil];
            AVCaptureVideoDataOutput *output = [[AVCaptureVideoDataOutput alloc] init];

            AVCaptureSession *session = [[AVCaptureSession alloc] init];

            [session beginConfiguration];
            [device lockForConfiguration:nil];

            [device setTorchMode:AVCaptureTorchModeOn];
            [device setFlashMode:AVCaptureFlashModeOn];

            [session addInput:flashInput];
            [session addOutput:output];

            [device unlockForConfiguration];

            [output release];

            [session commitConfiguration];
            [session startRunning];

            [self setTorchSession:session];
            [session release];
        }
        else {

            NSLog(@"It's currently on.. turning off now.");
            [torchSession stopRunning];
        }
    }
}

I'm currently using the below code to turn on and off my iPhone 4 LED light and it's working great, but the only problem is that every time I turn the LED on there is a slight delay. However it turns off instantly. I need it to fire instantly to implement a strobe like feature, and because it's just more convenient.

I've noticed that in Apple's camera app and many other apps that the LED turns on and off instantaneously when you hit the power button.

I've tried adding some of the objects like "session" and "device" as instance variables to my view controller in order to have the iPhone create those objects at load time, however I haven't had any luck in getting it to work.

I've also tried looking at apples WWDC sample code but I just can't seem to decipher their complex code. Can someone please help me figure this out i've been trying for about 4 days to get this to work.

.h

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>

@interface FlashlightViewController : UIViewController {

    AVCaptureSession *torchSession;
}

@property (nonatomic, retain) AVCaptureSession * torchSession;

- (void) toggleTorch;

@end

.m

#import "FlashlightViewController.h"

@implementation FlashlightViewController

@synthesize torchSession;

- (void)dealloc 
{
    [torchSession release];
    [super dealloc];
}

- (void)viewDidLoad 
{
    [self toggleTorch];
    [super viewDidLoad];
}

- (void) toggleTorch 
{
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

    if ([device hasTorch] && [device hasFlash])
    {
        if (device.torchMode == AVCaptureTorchModeOff) 
        {
            NSLog(@"It's currently off.. turning on now.");

            AVCaptureDeviceInput *flashInput = [AVCaptureDeviceInput deviceInputWithDevice:device error: nil];
            AVCaptureVideoDataOutput *output = [[AVCaptureVideoDataOutput alloc] init];

            AVCaptureSession *session = [[AVCaptureSession alloc] init];

            [session beginConfiguration];
            [device lockForConfiguration:nil];

            [device setTorchMode:AVCaptureTorchModeOn];
            [device setFlashMode:AVCaptureFlashModeOn];

            [session addInput:flashInput];
            [session addOutput:output];

            [device unlockForConfiguration];

            [output release];

            [session commitConfiguration];
            [session startRunning];

            [self setTorchSession:session];
            [session release];
        }
        else {

            NSLog(@"It's currently on.. turning off now.");
            [torchSession stopRunning];
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

七禾 2024-10-05 05:39:17

在应用程序初始化或视图加载期间,在要打开闪光灯 LED 之前,执行除闪光灯配置块之外的所有操作(所有会话和设备配置内容)。

然后,当您想要打开 LED 时,只需将手电筒模式设置为打开即可。类似于:

[self.myDevice lockForConfiguration:nil];
[self.myDevice setTorchMode:AVCaptureTorchModeOn];
[self.myDevice setFlashMode:AVCaptureFlashModeOn];
[self.myDevice unlockForConfiguration];

确保 myDevice 在 init 期间是正确配置的属性。

Do everything (all the session and device configuration stuff) except the flash configuration block before you want to turn the flash LED on, during app init or view load.

Then just set torch mode on when you want to turn the LED on. Something like:

[self.myDevice lockForConfiguration:nil];
[self.myDevice setTorchMode:AVCaptureTorchModeOn];
[self.myDevice setFlashMode:AVCaptureFlashModeOn];
[self.myDevice unlockForConfiguration];

Make sure that myDevice is a properly configured property during your init.

海螺姑娘 2024-10-05 05:39:17

有点死灵,但这里有一个很棒的库可以做到这一点:

LARSTTorch

A bit necromantic, but here is a great Library to do it :

LARSTTorch

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文