0

我正在使用下面的代码来打开 iphone 应用程序中的手电筒灯。它工作正常。问题是,当我们按下按钮时,手电筒模式将更改为“开”,但手电筒灯仅在用户进入相机屏幕时才会出现。我想在不使用相机屏幕的情况下打开手电筒。谁能指导我?请建议我哪里错了。这是我的代码,

captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    if (captureDevice.torchMode == AVCaptureTorchModeOff)  
    {
        AVCaptureSession *session = [[AVCaptureSession alloc] init];
        [session beginConfiguration];

        [captureDevice lockForConfiguration:nil];
        [captureDevice setTorchMode:AVCaptureTorchModeOn];
        [captureDevice unlockForConfiguration];

        [session commitConfiguration];
        [session startRunning];

        [self setTorchSession:session];
        [session release];
    }
    else 
    {
        [torchSession stopRunning];
        [captureDevice setTorchMode:AVCaptureTorchModeOff];
    }

这是 iPhone 中 Torch Light 的正确代码吗?请帮我。提前致谢。

4

1 回答 1

2

这段代码对我有用

- (void) internal_setFlashOn: (BOOL) turnOn {
  AVCaptureDevice *theDevice = self.captureDevice;

  if ([theDevice hasTorch]) {
    [theDevice lockForConfiguration: nil];
    AVCaptureTorchMode currentMode = [theDevice torchMode];
    BOOL isAlreadyTurnedOn = (AVCaptureTorchModeOn == currentMode);
    if (isAlreadyTurnedOn != turnOn) {
      [theDevice setTorchMode: turnOn? AVCaptureTorchModeOn: AVCaptureTorchModeOff];
    }

    [theDevice unlockForConfiguration];
  }
}

- (AVCaptureDevice *) captureDevice {
  if (nil == internal_captureDevice) {
    internal_captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    [internal_captureDevice retain];
  }
  return internal_captureDevice;
}

这适用于 iPhone4 及更高版本。

于 2011-12-20T11:25:37.370 回答