激活手电筒应用程序的LED

我正在尝试为我的iPhone制作一个手电筒应用程序。 我有一个iPhone 4,并希望利用我的iPhone上的LED为我的项目。 任何人都可以帮助我开始呢?

使用以下内容:

AVCaptureSession * session = [[AVCaptureSession alloc] init]; [session beginConfiguration]; AVCaptureDevice * device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; if ([device hasTorch] && [device hasFlash]){ [device lockForConfiguration:nil]; [device setTorchMode:AVCaptureTorchModeOn]; [device setFlashMode:AVCaptureFlashModeOn]; [device unlockForConfiguration]; AVCaptureDeviceInput * flashInput = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil]; if (flashInput){ [session addInput:flashInput]; } AVCaptureVideoDataOutput * output = [[AVCaptureVideoDataOutput alloc] init]; [session addOutput:output]; [output release]; [session commitConfiguration]; [session startRunning]; } [self setTorchSession:session]; [session release]; 

(从iPhoneDevSDK的讨论 )

这里是一个较短的版本,你现在可以用来打开或closuresLED:

 - (void)torchOnOff: (BOOL) onOff { AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; if ([device hasTorch]) { [device lockForConfiguration:nil]; [device setTorchMode: onOff ? AVCaptureTorchModeOn : AVCaptureTorchModeOff]; [device unlockForConfiguration]; } } 

更新:(2015年3月)

您还可以设置火炬的亮度:

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