2012-01-22 13 views
3

iPhone 4/4sのフラッシュをトーチとして使用する方法についての記事を見ました。 私はそれが非常に有用であることを発見しました、私はモールストランスミッタとしてそれを使用するためにLEDを素早くオン/オフするために使用しようとしましたが、動作しません。iPhone 4/4をモールストランスミッタとして使用

-(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ボタンを押す: それは私が使用しているコード以下、この使用のために遅すぎます。 誰でも手伝ってもらえますか?

+2

あなたは何を参照していますか? – BoltClock

+0

[iPhone 4 LEDライトを即座に鳴らすにはどうすればいいですか?](http://stackoverflow.com/questions/3983032/how-can-i-make-the-iphone-4-led-light-fire -instantly) – hotpaw2

答えて

4

ご使用の方法は、実際には非常に遅いです。代わりにthis Stackoverflowリンクにあるメソッドを使用してください。

実際のオン/オフのコードは次のとおりです。

[self.myDevice lockForConfiguration:nil]; 
[self.myDevice setTorchMode:AVCaptureTorchModeOn]; 
[self.myDevice setFlashMode:AVCaptureFlashModeOn]; 
[self.myDevice unlockForConfiguration]; 

しかしmyDeviceが初期化されていることを確認し(リンクを参照してください)

しかし限り、あなたが望むのフラッシュの長さを作成するためにNStimerとそれを実装します。

EDIT:

申し訳ありませんが、私はあなたがモールス信号の入力を収集しようとすると、その後NSTimerを切り替えるには、Flashを指示したと仮定しました。

NSTimerを試してください。または、スレッドをスリープして、点滅間隔の間隔を長くしてください。実際のフラッシュコンポーネントが処理するには速すぎます。テストの容易さのためにSOSメソッドでこれを試してください。

+0

多分私は誤解されている何かがありますが、あなたが私のswitchTorchOnメソッドを見れば、それはあなたが書いたものと同じです。私は変更する必要があります - (void)toggleTorch ??? – cursao

2

NSTimerで試しましたが、うまくいきませんでした。
最後に私は使用して解決しましたperformSelector:withObject:afterDelay:

関連する問題