iOS6手电筒不起作用

时间:2014-07-30 08:03:43

标签: ios objective-c xcode flashlight

我正在尝试制作hello-world-type app来学习如何打开/关闭手电筒。 所以这个应用程序中只有两个按钮:On和Off。 这是“开”按钮的动作:

AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
[device lockForConfiguration:nil];
[device setTorchMode:AVCaptureTorchModeOn];
[device unlockForConfiguration];

它在iOS7上运行正常,但在iOS6上不起作用。我做错了什么?

UPD:[device setFlashMode:AVCaptureFlashModeOn]无法正常工作

2 个答案:

答案 0 :(得分:0)

您似乎缺少一些步骤:

 - (void)toggleFlashlight
{
  AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

  if (device.torchMode == AVCaptureTorchModeOff)
  {
    // Create an AV session
    AVCaptureSession *session = [[AVCaptureSession alloc] init];

    // Create device input and add to current session
    AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error: nil];
    [session addInput:input];

    // Create video output and add to current session
    AVCaptureVideoDataOutput *output = [[AVCaptureVideoDataOutput alloc] init];
    [session addOutput:output];

    // Start session configuration
    [session beginConfiguration];
    [device lockForConfiguration:nil];

    // Set torch to on
    [device setTorchMode:AVCaptureTorchModeOn];

    [device unlockForConfiguration];
    [session commitConfiguration];

    // Start the session
    [session startRunning];

    // Keep the session around
    [self setAVSession:session];

    [output release];
  }
  else
  {
    [AVSession stopRunning];
    [AVSession release], AVSession = nil;
  }
}

希望它会对你有所帮助;)

PS:不是我的代码 - http://iosdevelopertips.com/camera/flashlight-application-using-the-iphone-led.html

答案 1 :(得分:0)

问题在于手机上的手电筒坏了:即使是相机应用也没有用手电筒拍照。我应该先检查一下这些硬件问题。