我正在制作一个应用程序,允许我将图像拼接成全景场景。我希望能够以编程方式打开 iPhone 4 上的闪光灯 LED。
我怎样才能做到这一点?
我阅读了文档并发现我需要使用 AVCaptureFlashMode
但我不知道 2 如何使用它?
任何帮助,将不胜感激。
下面更新了代码。谢谢SIF!
NSError* 错误 = 零;
NSLog(@"设置LED");
if([captDevice hasTorch] == NO)
{
NSLog(@"错误:此设备没有手电筒");
}
if([captDevice isTorchModeSupported:AVCaptureTorchModeOn] == NO)
{
NSLog(@"错误:此设备不支持 AVCaptureTorchModeOn");
}
AVCaptureSession* captureSession = [[AVCaptureSession alloc] init];
AVCaptureDeviceInput* videoInput = [[AVCaptureDeviceInput alloc] initWithDevice:captDevice error:&error];
AVCaptureVideoDataOutput* videoOutput = [[AVCaptureVideoDataOutput alloc] init];
如果(视频输入和视频输出)
{
[captureSession addInput:videoInput];
[captureSession addOutput:videoOutput];
if([captDevice lockForConfiguration:&error])
{
如果(标志 == 是){
captDevice.torchMode = AVCaptureTorchModeOn;
} 别的 {
captDevice.torchMode = AVCaptureTorchModeOff;
}
[captDevice unlockForConfiguration];
}
别的
{
NSLog(@"无法锁定设备配置错误:%@", error);
}
[捕获 session 开始运行];
}
别的
{
NSLog(@"错误:%@", 错误);
}
你怎么关掉它?
最佳答案
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];
}