[英]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.