[英]How to on Torch light without using camera/video mode in 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中Torch Light的正確代碼嗎? 請幫我。 提前致謝。
這段代碼適合我
- (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及更高版本。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.