如何使用AVCaptureFlashMode [英] How do I use AVCaptureFlashMode
本文介绍了如何使用AVCaptureFlashMode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在制作一个应用程序,让我将图像拼接成一个全景的场景。我想要能够以编程方式打开iPhone 4上的Flash LED。
我该如何做?
我阅读文档,发现我需要使用AVCaptureFlashMode
但我不知道如何使用它?
$
更新的代码如下。谢谢SIF!
NSError * error = nil;
NSLog(@设置LED);
if([captDevice hasTorch] == NO)
{
NSLog(@错误:此设备没有火炬);
}
if([captDevice isTorchModeSupported:AVCaptureTorchModeOn] == NO)
{
NSLog(@Error:This device does not support AVCaptureTorchModeOn);
}
AVCaptureSession * captureSession = [[AVCaptureSession alloc] init];
AVCaptureDeviceInput * videoInput = [[AVCaptureDeviceInput alloc] initWithDevice:captDevice错误:&错误];
AVCaptureVideoDataOutput * videoOutput = [[AVCaptureVideoDataOutput allocate] init];
if(videoInput && videoOutput)
{
[captureSession addInput:videoInput];
[captureSession addOutput:videoOutput];
if([captDevice lockForConfiguration:&error])
{
if(flag == YES){
captDevice.torchMode = AVCaptureTorchModeOn;
} else {
captDevice.torchMode = AVCaptureTorchModeOff;
}
[captDevice unlockForConfiguration];
}
else
{
NSLog(@无法锁定设备的配置错误:%@,错误);
}
[captureSession startRunning];
}
else
{
NSLog(@Error:%@,error);
}
如何关闭它?
解决方案
AVCaptureDevice * d = nil;
//按位置查找设备
NSArray * allDevices = [AVCaptureDevice devices];
for(AVCaptureDevice * currentDevice in allDevices){
if(currentDevice.position == AVCaptureDevicePositionBack){
d = currentDevice;
}
}
//此时,d可能仍为nil,假设我们发现了我们喜欢的东西....
NSError * err = nil;
BOOL lockAcquired = [d lockForConfiguration:& err];
if(!lockAcquired){
// log err and handle ...
} else {
//在闪光模式下翻转
if ([d hasFlash]&& [d isFlashModeSupported:AVCaptureFlashModeOn]){
[d setFlashMode:AVCaptureFlashModeOn];
}
[d unlockForConfiguration];
}
I'm making an app to allow me to stitch images together into a panoramic scene. I want to be able to turn the Flash LED on the iPhone 4 programatically.
How can I do this?
I read the documentation and discovered that I need to use AVCaptureFlashMode
but I can't figure out how 2 use it?
any help would be appreciated.
Updated Code below. Thanks SIF!
NSError* error = nil; NSLog(@"Setting up LED"); if([captDevice hasTorch] == NO) { NSLog(@"Error: This device doesnt have a torch"); } if([captDevice isTorchModeSupported:AVCaptureTorchModeOn] == NO) { NSLog(@"Error: This device doesnt support AVCaptureTorchModeOn"); } AVCaptureSession* captureSession = [[AVCaptureSession alloc] init]; AVCaptureDeviceInput* videoInput = [[AVCaptureDeviceInput alloc] initWithDevice:captDevice error:&error]; AVCaptureVideoDataOutput* videoOutput = [[AVCaptureVideoDataOutput alloc] init]; if (videoInput && videoOutput) { [captureSession addInput:videoInput]; [captureSession addOutput:videoOutput]; if([captDevice lockForConfiguration:&error]) { if (flag == YES) { captDevice.torchMode = AVCaptureTorchModeOn; } else { captDevice.torchMode = AVCaptureTorchModeOff; } [captDevice unlockForConfiguration]; } else { NSLog(@"Could not lock device for config error: %@", error); } [captureSession startRunning]; } else { NSLog(@"Error: %@", error); }
How do you turn it off?
解决方案
AVCaptureDevice* d = nil;
// find a device by position
NSArray* allDevices = [AVCaptureDevice devices];
for (AVCaptureDevice* currentDevice in allDevices) {
if (currentDevice.position == AVCaptureDevicePositionBack) {
d = currentDevice;
}
}
// at this point, d may still be nil, assuming we found something we like....
NSError* err = nil;
BOOL lockAcquired = [d lockForConfiguration:&err];
if (!lockAcquired) {
// log err and handle...
} else {
// flip on the flash mode
if ([d hasFlash] && [d isFlashModeSupported:AVCaptureFlashModeOn] ) {
[d setFlashMode:AVCaptureFlashModeOn];
}
[d unlockForConfiguration];
}
这篇关于如何使用AVCaptureFlashMode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文