简体   繁体   English

如何在不使用iPhone中的相机/视频模式的情况下使用手电筒?

[英]How to on Torch light without using camera/video mode in iPhone?

I am using this below code for switch on the Torch light in iphone app. 我正在使用以下代码在iPhone应用程序中打开手电筒灯。 It working fine. 它工作正常。 The issue is, when we press the button the torch mode will be changed to 'On' but, the torch light only appear when the user entering into the Camera screen. 问题是,当我们按下按钮时,手电筒模式将变为“开”,但只有当用户进入相机屏幕时才会出现手电筒灯。 I want to switch on the torch light without using the Camera screen. 我想在不使用相机屏幕的情况下打开手电筒灯。 Can anyone please guide me? 有人可以指导我吗? Please suggest me where i am wrong. 请建议我在哪里错了。 Here my code, 这是我的代码,

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];
    }

Is this correct code for Torch Light in iPhone? 这是iPhone中Torch Light的正确代码吗? Please help me. 请帮我。 Thanks in advance. 提前致谢。

This code works for me 这段代码适合我

- (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;
}

This works on iPhone4 and above. 这适用于iPhone4及更高版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM