打开 iPhone 上的手电筒/闪光灯
我知道在 iPhone 4 上打开闪光灯并保持打开状态的唯一方法是打开摄像机。不过我不太确定代码。这就是我正在尝试的:
-(IBAction)turnTorchOn {
AVCaptureSession *captureSession = [[AVCaptureSession alloc] init];
AVCaptureDevice *videoCaptureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
NSError *error = nil;
AVCaptureDeviceInput *videoInput = [AVCaptureDeviceInput deviceInputWithDevice:videoCaptureDevice error:&error];
if (videoInput) {
[captureSession addInput:videoInput];
AVCaptureVideoDataOutput* videoOutput = [[AVCaptureVideoDataOutput alloc] init];
[videoOutput setSampleBufferDelegate:self queue:dispatch_get_current_queue()];
[captureSession addOutput:videoOutput];
[captureSession startRunning];
videoCaptureDevice.torchMode = AVCaptureTorchModeOn;
}
}
有人知道这是否有效或者我错过了什么吗? (我还没有 iPhone 4 来测试 - 只是尝试一些新的 API)。
谢谢
I know that the only way to turn on the flash and keep it on on iPhone 4 is by turning the video camera on. I'm not too sure of the code though. Here is what I am trying:
-(IBAction)turnTorchOn {
AVCaptureSession *captureSession = [[AVCaptureSession alloc] init];
AVCaptureDevice *videoCaptureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
NSError *error = nil;
AVCaptureDeviceInput *videoInput = [AVCaptureDeviceInput deviceInputWithDevice:videoCaptureDevice error:&error];
if (videoInput) {
[captureSession addInput:videoInput];
AVCaptureVideoDataOutput* videoOutput = [[AVCaptureVideoDataOutput alloc] init];
[videoOutput setSampleBufferDelegate:self queue:dispatch_get_current_queue()];
[captureSession addOutput:videoOutput];
[captureSession startRunning];
videoCaptureDevice.torchMode = AVCaptureTorchModeOn;
}
}
Does anybody know if this would work or am I missing anything? (I don't have an iPhone 4 yet to test on -just trying out some of the new API's).
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您现在可以使用以下较短版本来打开或关闭灯:
更新:(2015 年 3 月)
使用 iOS 6.0 及更高版本,您可以使用以下方法控制手电筒的亮度或级别:
您可能还想监控从
setTorchModeOnWithLevel:
返回值 (success
)。如果您尝试将级别设置得太高并且割炬过热,则可能会失败。在这种情况下,将级别设置为 AVCaptureMaxAvailableTorchLevel 会将级别设置为给定割炬温度所允许的最高级别。Here's a shorter version you can now use to turn the light on or off:
UPDATE: (March 2015)
With iOS 6.0 and later, you can control the brightness or level of the torch using the following method:
You may also want to monitor the return value (
success
) fromsetTorchModeOnWithLevel:
. You may get a failure if you try to set the level too high and the torch is overheating. In that case setting the level toAVCaptureMaxAvailableTorchLevel
will set the level to the highest level that is allowed given the temperature of the torch.iWasRobbed 的答案很好,除了有一个 AVCaptureSession 一直在后台运行。根据 Instrument 的数据,在我的 iPhone 4s 上,它占用了大约 12% 的 CPU 电量,因此我的应用程序在一分钟内消耗了大约 1% 的电量。换句话说,如果该设备准备用于 AV 捕获,那么它的价格并不便宜。
使用我的应用程序下面的代码每分钟需要 0.187% 的电量,因此电池寿命延长了 5 倍以上。
该代码在任何设备上都可以正常工作(在 3GS(无闪存)和 4s 上进行了测试)。也在模拟器中的 4.3 上进行了测试。
iWasRobbed's answer is great, except there is an AVCaptureSession running in the background all the time. On my iPhone 4s it takes about 12% CPU power according to Instrument so my app took about 1% battery in a minute. In other words if the device is prepared for AV capture it's not cheap.
Using the code below my app requires 0.187% a minute so the battery life is more than 5x longer.
This code works just fine on any device (tested on both 3GS (no flash) and 4s). Tested on 4.3 in simulator as well.
在下面查看更好的答案: https://stackoverflow.com/a/10054088/308315
旧答案:
首先,在您的 AppDelegate .h 文件中:
然后在您的 AppDelegate .m 文件中:
然后,只要您想打开它,只需执行以下操作:
与关闭它类似:
See a better answer below: https://stackoverflow.com/a/10054088/308315
Old answer:
First, in your AppDelegate .h file:
Then in your AppDelegate .m file:
Then anytime you want to turn it on, just do something like this:
And similar for turning it off:
lockforConfiguration
在您的代码中设置,您在其中声明AVCaptureDevice
是一个属性。the
lockforConfiguration
is set in your code, where you declare yourAVCaptureDevice
is a property.从 iOS 6.0 及更高版本开始,切换手电筒闪光灯开/关,
PS 仅当您没有开/关功能时才建议使用此方法。请记住,还有一个选项
自动
。即AVCaptureFlashModeAuto
和AVCaptureTorchModeAuto
。为了支持自动模式,您需要跟踪当前模式并根据该模式更改闪光灯和闪光灯模式。火炬。From iOS 6.0 and above, toggling torch flash on/off,
P.S. This approach is only suggestible if you don't have on/off function. Remember there's one more option
Auto
. i.e.AVCaptureFlashModeAuto
andAVCaptureTorchModeAuto
. To support auto mode as well, you've keep track of current mode and based on that change mode of flash & torch.斯威夫特2.0版本:
Swift 2.0 version:
这项工作非常好..希望它对某人有帮助!
This work's very well.. hope it help's someone !