【问题标题】:How to on Torch light without using camera/video mode in iPhone?如何在不使用 iPhone 中的相机/视频模式的情况下打开手电筒?
【发布时间】:2011-12-20 11:15:04
【问题描述】:

我正在使用下面的代码来打开 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 的正确代码吗?请帮我。提前致谢。

【问题讨论】:

    标签: iphone light avcapturedevice


    【解决方案1】:

    此代码适用于我

    - (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 及更高版本。

    【讨论】:

    • 感谢您的现场回复。我有一个澄清,请清除我。如果打开手电筒灯,它会出现在原地,或者当相机屏幕拾取时灯会出现。我已将 AVCaptureDevice 类型的视频更改为文本。这是工作吗?谢谢。
    • 我认为在你的代码中我不需要使用相机(UIImagePickerController)。它是否正确。请帮我。谢谢。
    • 是的,在 iPhone4 上可以在不显示相机屏幕的情况下使用
    猜你喜欢
    • 1970-01-01
    • 2013-01-26
    • 2019-12-10
    • 2020-06-04
    • 1970-01-01
    • 2021-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多