使用iPhone4/4s作为莫尔斯发射机
我看到了一篇关于使用iPhone4/4s闪光led作为火炬的帖子。 我发现它非常有用,我试着用它来快速打开/关闭led,以便将其用作莫尔斯发射机,但它不起作用。 按照我使用的代码,此使用速度太慢:使用iPhone4/4s作为莫尔斯发射机,iphone,ios,Iphone,Ios,我看到了一篇关于使用iPhone4/4s闪光led作为火炬的帖子。 我发现它非常有用,我试着用它来快速打开/关闭led,以便将其用作莫尔斯发射机,但它不起作用。 按照我使用的代码,此使用速度太慢: -(void)toggleTorch { AVCaptureDevice *_device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; // check if the device has the
-(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按钮时,我只看到一个闪光。
有人可以帮我吗?你使用的方法确实很慢。请改用Stackoverflow链接中的方法 实际开/关代码为:
[self.myDevice lockForConfiguration:nil];
[self.myDevice setTorchMode:AVCaptureTorchModeOn];
[self.myDevice setFlashMode:AVCaptureFlashModeOn];
[self.myDevice unlockForConfiguration];
但请确保myDevice
已初始化(请参阅链接)
使用NStimer
实现该功能,以创建任意长度的闪存
编辑:
抱歉,我假设您正在尝试收集莫尔斯电码输入,然后指示闪光灯在NSTimer
之间切换
尝试使用
NSTimer
或休眠线程以增加闪烁间隔之间的时间。它的执行速度可能太快,实际的闪存组件无法处理。为了便于测试,请在SOS方法上尝试此方法。我尝试了NSTimer
,但没有成功。最后,我用
performSelector:withObject:afterDelay:
解决了这个问题。你指的是什么帖子?可能是重复的,可能是我误解了什么,但如果你看看我的switchTorchOn方法,它和你写的是一样的,除了一些检查。我必须改变-(无效)切换火炬???