2011-12-20 72 views
0

我正在使用下面的代碼來切換iphone應用程序中的手電筒燈。它工作正常。問題是,當我們按下按鈕時,手電筒模式將變爲「開」,但手電筒僅在用戶進入相機屏幕時出現。我想在不使用相機屏幕的情況下打開手電筒燈。任何人都可以請指導我?請告訴我我錯在哪裏。在這裏我的代碼,如何在iPhone中使用相機/視頻模式而不使用手電筒?

captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; 
    if (captureDevice.torchMode == AVCaptureTorchModeOff) 
    { 
     AVCaptureSession *session = [[AVCaptureSession alloc] init]; 
     [session beginConfiguration]; 

     [captureDevice lockForConfiguration:nil]; 
     [captureDevice setTorchMode:AVCaptureTorchModeOn]; 
     [captureDevice unlockForConfiguration]; 

     [session commitConfiguration]; 
     [session startRunning]; 

     [self setTorchSession:session]; 
     [session release]; 
    } 
    else 
    { 
     [torchSession stopRunning]; 
     [captureDevice setTorchMode:AVCaptureTorchModeOff]; 
    } 

是對手電筒在iPhone這個正確的代碼?請幫幫我。提前致謝。

回答

2

此代碼對我的作品

- (void) internal_setFlashOn: (BOOL) turnOn { 
    AVCaptureDevice *theDevice = self.captureDevice; 

    if ([theDevice hasTorch]) { 
    [theDevice lockForConfiguration: nil]; 
    AVCaptureTorchMode currentMode = [theDevice torchMode]; 
    BOOL isAlreadyTurnedOn = (AVCaptureTorchModeOn == currentMode); 
    if (isAlreadyTurnedOn != turnOn) { 
     [theDevice setTorchMode: turnOn? AVCaptureTorchModeOn: AVCaptureTorchModeOff]; 
    } 

    [theDevice unlockForConfiguration]; 
    } 
} 

- (AVCaptureDevice *) captureDevice { 
    if (nil == internal_captureDevice) { 
    internal_captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; 
    [internal_captureDevice retain]; 
    } 
    return internal_captureDevice; 
} 

這適用於iPhone4和上面。

+0

感謝您的現場迴應。我有一個澄清請澄清我。如果打開手電筒燈,它會出現在光點處,或者當相機屏幕將被選中時,光線會出現。我已將AVCaptureDevice類型的視頻更改爲文本。這是否工作?謝謝。 – Gopinath 2011-12-20 11:29:27

+0

我想你的代碼我不需要使用相機(UIImagePickerController)。它是否正確。請幫幫我。謝謝。 – Gopinath 2011-12-20 11:32:11

+0

是的,在iPhone4上這個工作原理沒有顯示相機屏幕 – Denis 2011-12-20 13:04:10

相關問題