2011-12-20 117 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