打开iPhone上的手电筒/闪光灯

我知道打开闪光灯并将其保持在iPhone 4上的唯一方法是打开摄像机。 虽然我不太清楚代码。 这是我正在尝试的:

-(IBAction)turnTorchOn { AVCaptureSession *captureSession = [[AVCaptureSession alloc] init]; AVCaptureDevice *videoCaptureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; NSError *error = nil; AVCaptureDeviceInput *videoInput = [AVCaptureDeviceInput deviceInputWithDevice:videoCaptureDevice error:&error]; if (videoInput) { [captureSession addInput:videoInput]; AVCaptureVideoDataOutput* videoOutput = [[AVCaptureVideoDataOutput alloc] init]; [videoOutput setSampleBufferDelegate:self queue:dispatch_get_current_queue()]; [captureSession addOutput:videoOutput]; [captureSession startRunning]; videoCaptureDevice.torchMode = AVCaptureTorchModeOn; } } 

有人知道这是否会工作,或者我错过了什么? (我还没有iPhone 4的testing – 只是尝试一些新的API)。

谢谢

lockforConfiguration在您的代码中设置,您声明您的AVCaptureDevice是一个属性。

 [videoCaptureDevice lockForConfiguration:nil]; 

以下是您可以用来打开或closures指示灯的较短版本:

 AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; if ([device hasTorch]) { [device lockForConfiguration:nil]; [device setTorchMode:AVCaptureTorchModeOn]; // use AVCaptureTorchModeOff to turn off [device unlockForConfiguration]; } 

更新:(2015年3月)

使用iOS 6.0及更高版本,您可以使用以下方法控制手电筒的亮度或等级:

 - (void)setTorchToLevel:(float)torchLevel { AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; if ([device hasTorch]) { [device lockForConfiguration:nil]; if (torchLevel <= 0.0) { [device setTorchMode:AVCaptureTorchModeOff]; } else { if (torchLevel >= 1.0) torchLevel = AVCaptureMaxAvailableTorchLevel; BOOL success = [device setTorchModeOnWithLevel:torchLevel error:nil]; } [device unlockForConfiguration]; } } 

您可能还想要监视来自setTorchModeOnWithLevel:的返回值( successsetTorchModeOnWithLevel: 如果您尝试将电平设置得太高而手电筒过热,则可能会失败。 在这种情况下,将等级设置为AVCaptureMaxAvailableTorchLevel会将等级设置为考虑到火炬的温度所允许的最高等级。

iWasRobbed的答案很好,除了一直在后台运行的AVCaptureSession 。 在我的iPhone 4s上,根据乐器, CPU的功耗大约为12% ,所以我的应用在一分钟内耗电约1%。 换句话说,如果设备准备用于AV捕捉,则不便宜。

使用下面的代码我的应用程序需要0.187%一分钟,所以电池寿命是5倍以上。

这个代码在任何设备上都可以正常工作(在3GS(无闪存)和4s上testing)。 在模拟器中testing4.3。

 #import <AVFoundation/AVFoundation.h> - (void) turnTorchOn:(BOOL)on { Class captureDeviceClass = NSClassFromString(@"AVCaptureDevice"); if (captureDeviceClass != nil) { AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; if ([device hasTorch] && [device hasFlash]){ [device lockForConfiguration:nil]; if (on) { [device setTorchMode:AVCaptureTorchModeOn]; [device setFlashMode:AVCaptureFlashModeOn]; torchIsOn = YES; } else { [device setTorchMode:AVCaptureTorchModeOff]; [device setFlashMode:AVCaptureFlashModeOff]; torchIsOn = NO; } [device unlockForConfiguration]; } } } 

请参阅下面的更好的答案: https : //stackoverflow.com/a/10054088/308315


老答案:

首先,在您的AppDelegate .h文件中:

 #import <AVFoundation/AVFoundation.h> @interface AppDelegate : NSObject <UIApplicationDelegate> { AVCaptureSession *torchSession; } @property (nonatomic, retain) AVCaptureSession * torchSession; @end 

然后在你的AppDelegate .m文件中:

 @implementation AppDelegate @synthesize torchSession; - (void)dealloc { [torchSession release]; [super dealloc]; } - (id) init { if ((self = [super init])) { // initialize flashlight // test if this class even exists to ensure flashlight is turned on ONLY for iOS 4 and above Class captureDeviceClass = NSClassFromString(@"AVCaptureDevice"); if (captureDeviceClass != nil) { AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; if ([device hasTorch] && [device hasFlash]){ if (device.torchMode == AVCaptureTorchModeOff) { NSLog(@"Setting up flashlight for later use..."); AVCaptureDeviceInput *flashInput = [AVCaptureDeviceInput deviceInputWithDevice:device error: nil]; AVCaptureVideoDataOutput *output = [[AVCaptureVideoDataOutput alloc] init]; AVCaptureSession *session = [[AVCaptureSession alloc] init]; [session beginConfiguration]; [device lockForConfiguration:nil]; [session addInput:flashInput]; [session addOutput:output]; [device unlockForConfiguration]; [output release]; [session commitConfiguration]; [session startRunning]; [self setTorchSession:session]; [session release]; } } } } return self; } 

然后任何时候你想打开它,只要做这样的事情:

 // test if this class even exists to ensure flashlight is turned on ONLY for iOS 4 and above Class captureDeviceClass = NSClassFromString(@"AVCaptureDevice"); if (captureDeviceClass != nil) { AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; [device lockForConfiguration:nil]; [device setTorchMode:AVCaptureTorchModeOn]; [device setFlashMode:AVCaptureFlashModeOn]; [device unlockForConfiguration]; } 

类似的closures:

 // test if this class even exists to ensure flashlight is turned on ONLY for iOS 4 and above Class captureDeviceClass = NSClassFromString(@"AVCaptureDevice"); if (captureDeviceClass != nil) { AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; [device lockForConfiguration:nil]; [device setTorchMode:AVCaptureTorchModeOff]; [device setFlashMode:AVCaptureFlashModeOff]; [device unlockForConfiguration]; } 

我写了一个适用于Cordova 2.2.0的Torch插件。 你可以在这里find它:

https://github.com/tomschreck/iOS-Torch-Plugin

从iOS 6.0及以上版本开启/closures手电筒闪光灯,

 - (void) toggleFlash { AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; if ([device hasTorch] && [device hasFlash]){ [device lockForConfiguration:nil]; [device setFlashMode:(device.flashActive) ? AVCaptureFlashModeOff : AVCaptureFlashModeOn]; [device setTorchMode:(device.torchActive) ? AVCaptureTorchModeOff : AVCaptureTorchModeOn]; [device unlockForConfiguration]; } } 

PS如果你没有开/关function,这种方法只能expression出来。 请记住,还有一个选项“ Auto 。 即AVCaptureFlashModeAutoAVCaptureTorchModeAuto 。 为了支持自动模式,您还可以跟踪当前模式,并基于闪光灯和手电筒的更改模式。

Swift 2.0版本:

 func setTorchLevel(torchLevel: Float) { self.captureSession?.beginConfiguration() defer { self.captureSession?.commitConfiguration() } if let device = backCamera?.device where device.hasTorch && device.torchAvailable { do { try device.lockForConfiguration() defer { device.unlockForConfiguration() } if torchLevel <= 0.0 { device.torchMode = .Off } else if torchLevel >= 1.0 { try device.setTorchModeOnWithLevel(min(torchLevel, AVCaptureMaxAvailableTorchLevel)) } } catch let error { print("Failed to set up torch level with error \(error)") return } } } 
 //import fremework in .h file #import <AVFoundation/AVFoundation.h> { AVCaptureSession *torchSession; } @property(nonatomic,retain)AVCaptureSession *torchSession; -(IBAction)onoff:(id)sender; //implement in .m file @synthesize torchSession; -(IBAction)onoff:(id)sender { AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; if ([device hasTorch] && [device hasFlash]) { if (device.torchMode == AVCaptureTorchModeOff) { [button setTitle:@"OFF" forState:UIControlStateNormal]; 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 { [button setTitle:@"ON" forState:UIControlStateNormal]; [torchSession stopRunning]; } } } - (void)dealloc { [torchSession release]; [super dealloc]; } 

这个工作很好..希望能帮助到别人!

 -(IBAction)flashlight:(id)sender { AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; if ([device hasTorch] && [device hasFlash]){ if (device.torchMode == AVCaptureTorchModeOff) { [sender setTitle:@"Torch Off" forState:UIControlStateNormal]; AVCaptureDeviceInput *flashInput = [AVCaptureDeviceInput deviceInputWithDevice:device error: nil]; AVCaptureVideoDataOutput *output = [[AVCaptureVideoDataOutput alloc] init]; AVCaptureSession *cam = [[AVCaptureSession alloc] init]; [cam beginConfiguration]; [device lockForConfiguration:nil]; [device setTorchMode:AVCaptureTorchModeOn]; [device setFlashMode:AVCaptureFlashModeOn]; [cam addInput:flashInput]; [cam addOutput:output]; [device unlockForConfiguration]; [cam commitConfiguration]; [cam startRunning]; [self setTorchSession:cam]; } else { [sender setTitle:@"Torch On" forState:UIControlStateNormal]; [_torchSession stopRunning]; } } }