2011-08-09 157 views
4

我有一個開關,如果我激活它,我打開相機閃光燈,如果關閉,關閉(默認爲關閉)問題與相機閃光燈iphone

這是我的代碼:

- (void)viewDidAppear:(BOOL)animated 

{ 

    if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera] == NO) 
     return; 

    picker = [[UIImagePickerController alloc] init]; 
    picker.sourceType = UIImagePickerControllerSourceTypeCamera; 

    picker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType: UIImagePickerControllerCameraCaptureModeVideo]; 

    picker.allowsEditing = NO; 
    picker.showsCameraControls = NO; 

    picker.delegate = self; 

    [self presentModalViewController:picker animated:YES]; 

} 

- (IBAction) onChangeSwitch:(id)sender 

{ 

    switch(interruptor.on){ 
     case YES: 
      picker.cameraFlashMode = UIImagePickerControllerCameraFlashModeOn; 
      break; 
     case NO: 
      picker.cameraFlashMode = UIImagePickerControllerCameraFlashModeOff; 
      break; 
    } 
} 

在線看,我已經看到我的代碼是簡單地轉動閃光燈,而不是從iPhone攝像頭啓動或停止手電筒。

我已經看到它用AVCaptureDevice完成Turn on torch/flash on iPhone#3367424我不知道現在怎麼能適應我的代碼。

有誰知道並給我一隻手?

感謝

回答

2

這裏是我打開燈(又名火炬)上&關:

- (void) setTorchOn:(BOOL)isOn 
{ 
    AVCaptureDevice* device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; 
    [device lockForConfiguration:nil]; //you must lock before setting torch mode 
    [device setTorchMode:isOn ? AVCaptureTorchModeOn : AVCaptureTorchModeOff]; 
    [device unlockForConfiguration]; 
} 

我敢肯定你會需要鏈接到AVFoundation框架。

+0

非常感謝。 我會嘗試看看我是否有運氣,但我認爲如果我有選擇器,我不能與AvCaputre混合...看看我是否有運氣。 非常感謝你。 – jekeyeke

3
- (void)flashLightOn { 


    NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]; 
    for (AVCaptureDevice *device in devices) { 
     if ([device hasFlash] == YES) { 

      [device lockForConfiguration:nil]; 
      [device setTorchMode:AVCaptureTorchModeOn]; 
      [device unlockForConfiguration]; 
     } 

    } 
} 

-(void)flashLightOff { 


    NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]; 
    for (AVCaptureDevice *device in devices) { 
     if ([device hasFlash] == YES) { 

      [device lockForConfiguration:nil]; 
      [device setTorchMode:AVCaptureTorchModeOff]; 
      [device unlockForConfiguration]; 
     } 

    } 

}