如何让iPhone 4 LED瞬间点亮? [英] How can I make the iPhone 4 LED light fire instantly?

查看:134
本文介绍了如何让iPhone 4 LED瞬间点亮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用以下代码打开和关闭我的iPhone 4 LED灯,它运行良好,但唯一的问题是,每次我打开LED时都会有轻微的延迟。然而,它立即关闭。我需要它立即启动以实现类似频闪的功能,因为它更方便。

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.

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

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.

我尝试将一些像session和device这样的对象作为实例变量添加到我的视图控制器中,以便让iPhone在加载时创建这些对象时间,但是我没有运气让它运转起来。

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.

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

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.

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

@interface FlashlightViewController : UIViewController {

    AVCaptureSession *torchSession;
}

@property (nonatomic, retain) AVCaptureSession * torchSession;

- (void) toggleTorch;

@end



.m



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


推荐答案

在app init或view load期间打开闪光灯LED之前,请执行除闪存配置块之外的所有操作(所有会话和设备配置)。

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.

然后只需在打开LED时设置割炬模式。类似于:

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];

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

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

这篇关于如何让iPhone 4 LED瞬间点亮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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