4

我正在制作一个应用程序,让我可以将图像拼接成一个全景场景。我希望能够以编程方式打开 iPhone 4 上的 Flash 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(@"Could not lock device for config error: %@", error);
        }
        [captureSession 开始运行];
    }
    别的
    {
        NSLog(@"错误:%@", 错误);
    }

你怎么关掉它?

4

1 回答 1

7
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];
}
于 2010-07-08T13:31:44.607 回答