我正在制作一个应用程序,让我把图像缝合到一个全景场景中。我希望以编目地将闪光灯导在iPhone 4上。

如何这样做?

我读取了文档并发现我需要使用avcaptureflashmode

但我无法弄清楚如何使用它?

任何帮助将是值得注意的。


更新了下面的代码。谢谢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);
    }
.

如何关闭它?

有帮助吗?

解决方案

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];
}
.
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top