2012-01-22 30 views
3

我看到關於使用iPhone 4/4s閃光燈作爲火炬的帖子。 我發現它非常有用,我試圖用它來快速打開/關閉LED,以便將它用作莫爾斯發射器,但它不起作用。 正是由於這個使用太慢,我使用的代碼如下:使用iPhone 4/4s作爲莫爾斯發射器

-(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實現它來創建的,但是你想長閃長。

編輯:

對不起,我以爲你是試圖收集的莫爾斯電碼輸入,然後指示閃光燈通過NSTimer切換。

嘗試使用NSTimer或休眠線程以增加閃爍間隔之間的時間。它可能執行得太快而無法處理實際的Flash組件。爲了便於測試,請嘗試使用SOS方法。

+0

也許有些東西我誤解了,但是如果你看看我的switchTorchOn方法,它和你寫的一樣,除了一些檢查。我必須改變 - (void)toggleTorch ??? – cursao

2

我試過NSTimer,但沒有奏效。
最後我解決了使用performSelector:withObject:afterDelay: