我看到关于使用iPhone 4 / 4s闪光灯作为手电筒的帖子。 我发现它非常有用,我试图用它来快速打开/关闭LED,以便将它用作莫尔斯变送器,但它不起作用。 根据我使用的代码,这个用途太慢了:
-(void)toggleTorch
{
AVCaptureDevice *_device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
// check if the device has the torch
if([_device hasTorch] && [_device hasFlash])
{
if (_device.torchMode == AVCaptureTorchModeOff)
{
// we want to turn the torch on
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];
}
else
{
[self.torchSession stopRunning];
}
}
}
// turn the torch on/off
-(IBAction)toggleTorch:(id)sender
{
AVCaptureDevice *_device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
// check if the device has the torch
if([_device hasTorch] && [_device hasFlash])
{
if (_device.torchMode == AVCaptureTorchModeOff)
{
[self switchTorchON];
}
else
{
[self switchTorchOFF];
}
}
}
-(void)switchTorchON
{
[NSThread detachNewThreadSelector:@selector(changeSwitchImage)
toTarget:self
withObject:nil];
Class captureDeviceClass = NSClassFromString(@"AVCaptureDevice");
if (captureDeviceClass != nil) {
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
[device lockForConfiguration:nil];
[device setTorchMode:AVCaptureTorchModeOn];
[device setFlashMode:AVCaptureFlashModeOn];
[device unlockForConfiguration];
}
}
-(void)switchTorchOFF
{
[NSThread detachNewThreadSelector:@selector(changeSwitchImage)
toTarget:self
withObject:nil];
// 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];
}
}
-(IBAction)toggleSOS:(id)sender
{
// morse SOS: ...---...
[self switchTorchON];
[self switchTorchOFF];
[self switchTorchON];
[self switchTorchOFF];
[self switchTorchON];
[self switchTorchOFF];
}
当我按下sos按钮时,我看到一个闪光灯。 有人可以帮帮我吗?
答案 0 :(得分:4)
您使用的方法确实很慢。请改用this Stackoverflow链接中的方法。
实际的开/关代码是:
[self.myDevice lockForConfiguration:nil];
[self.myDevice setTorchMode:AVCaptureTorchModeOn];
[self.myDevice setFlashMode:AVCaptureFlashModeOn];
[self.myDevice unlockForConfiguration];
但请确保myDevice
已初始化(请参阅链接)
使用NStimer
实现它,以创建您想要的闪存长度。
编辑:
抱歉,我认为您试图收集摩尔斯电码输入,然后指示闪光灯切换NSTimer
。
尝试使用NSTimer
或睡眠线程以增加闪烁间隔之间的时间。它可能执行得太快,无法处理实际的闪存组件。在SOS方法上试一试,以便于测试。
答案 1 :(得分:2)
我尝试使用NSTimer
,但它没有用。
最后,我使用performSelector:withObject:afterDelay: