2012-10-30 86 views
6

我正在開發一個簡單的應用程序,在該應用程序中,我需要使相機閃光燈模式不僅在拍攝圖像時持續亮起。操作模式應該是相機不錄像。可能嗎 ?如果是這樣,比如何。請用一些代碼幫助我如何用閃光燈模式打開相機

+0

此鏈接可能幫你。 [如何訪問照相機閃光燈] [1] [1]:http://stackoverflow.com/questions/4233100/how-to-access-camera-flash-with-xcode- uiimagepickercontroller –

回答

8

您可以使用以下方法打開和關閉相機閃光燈。

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

您還可以使用下面的方法用相機的顯示屏一起,

- (void) toggleFlashlight { 

// check if flashlight available 
Class captureDeviceClass = NSClassFromString(@"AVCaptureDevice"); 
if (captureDeviceClass != nil) { 
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; 
    if ([device hasTorch] && [device hasFlash]){ 

     [device lockForConfiguration:nil]; 
     if (device.torchMode == AVCaptureTorchModeOff) 
     { 
      [device setTorchMode:AVCaptureTorchModeOn]; 
      [device setFlashMode:AVCaptureFlashModeOn]; 

     } 
     else 
     { 
      [device setTorchMode:AVCaptureTorchModeOff]; 
      [device setFlashMode:AVCaptureFlashModeOff]; 

     } 
     [device unlockForConfiguration]; 
    } 
} 
} 

Source

+0

@The_code_cracker,這是你在找什麼? – iDev

+1

請務必檢查設備是否具有割炬功能;-) 'BOOL deviceHasFlash =([device hasTorch] && [device hasFlash]);' – atxe

+0

@atxe,這是真的。應該檢查。但是,由於他打算使用閃光燈模式,我假設已經檢查過。這個關於代碼的 – iDev

2

使用此爲雨燕4

func toggleFlash() { 
    guard let device = AVCaptureDevice.default(for: AVMediaType.video) 
     else {return} 

    if device.hasTorch { 
     do { 
      try device.lockForConfiguration() 
      if device.torchMode == AVCaptureDevice.TorchMode.on { 
       device.torchMode = AVCaptureDevice.TorchMode.off 
        //AVCaptureDevice.TorchModeAVCaptureDevice.TorchMode.off 
      } else { 
       do { 
        try device.setTorchModeOn(level: 1.0) 
       } catch { 
        print(error) 
       } 
      } 
      device.unlockForConfiguration() 
     } catch { 
      print(error) 
     } 
    } 
} 
相關問題