我正在使用以下代码在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正确的火炬之光代码吗?请帮我。提前谢谢。
答案 0 :(得分: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及更高版本。