2010-11-20 76 views
5

我想知道如何使用UIImagePickerController打開iPhone 4上的相機閃光燈。如何訪問UIImagePickerController上的相機閃光燈?

if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerCameraDeviceFront] == YES) 
{ 
    /// What code here /// 
} 

else 
{ 
    NoFlash = [[UIAlertView alloc] initWithTitle:@"Uh-Oh" 
             message:@"Your device doesn't have a flash camera" 
             delegate:nil 
           cancelButtonTitle:@"mhmm, OK" 
           otherButtonTitles:nil]; 
    NoFlash.delegate = self; 
    [NoFlash show]; 
    [NoFlash release]; 
} 

}

我已經在這裏讀的UIImagePickerController類參考網頁:http://bit.ly/cdAhhB但我沒有找到答案。有人可以幫幫我嗎?

謝謝

+0

這有什麼好做的Xcode。我要重播這個。 – 2010-11-20 18:25:07

回答

4

您可以使用它。當你想打開或關閉閃光燈時,基本上稱爲「toggleTorch」。希望這是你正在尋找的。

- (void) toggleTorch { 

    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; 

    if ([device hasTorch] && [device hasFlash]){ 

     if (device.torchMode == AVCaptureTorchModeOff) { 

      NSLog(@"It's currently off.. turning on now."); 

      [power setImage:[UIImage imageNamed:@"[email protected]"] forState:UIControlStateNormal]; 

      AVCaptureDeviceInput *flashInput = [AVCaptureDeviceInput deviceInputWithDevice:device error: nil]; 
      AVCaptureVideoDataOutput *output = [[AVCaptureVideoDataOutput alloc] init]; 

      AVCaptureSession *session = [[AVCaptureSession alloc] init]; 

      [session beginConfiguration]; 
      [device lockForConfiguration:nil]; 

      [device setTorchMode:AVCaptureTorchModeOn]; 
      [device setFlashMode:AVCaptureFlashModeOn]; 

      [session addInput:flashInput]; 
      [session addOutput:output]; 

      [device unlockForConfiguration]; 

      [output release]; 

      [session commitConfiguration]; 
      [session startRunning]; 

      [self setTorchSession:session]; 
      [session release]; 
     } 
     else { 

      NSLog(@"It's currently on.. turning off now."); 

      [power.imageView setImage:[UIImage imageNamed:@"[email protected]"]]; 

      [torchSession stopRunning]; 

     } 

    } 

} 

-(IBAction)powerBtn 
{ 
    [self toggleTorch]; 
} 
+0

我會嘗試。謝謝! ; D – Aluminum 2010-11-20 15:05:24

+3

這是用於火炬的,與閃光無關。只需更改閃光燈的PickerController.cameraFlashMode即可。 – Fattie 2014-02-28 19:25:43

7
-(void)flashSelected 
{ 

if (PickerController.cameraFlashMode == 
UIImagePickerControllerCameraFlashModeOff) { 

    if ([UIImagePickerController 
isFlashAvailableForCameraDevice:UIImagePickerControllerCameraDeviceRear ]) 

    { 
     PickerController.cameraFlashMode = 
UIImagePickerControllerCameraFlashModeOn; 
    } 
} 
else 
{ 
    PickerController.cameraFlashMode = 
UIImagePickerControllerCameraFlashModeOff; 
}  
} 

交替..

-(void)_flashToggle 
{ 
if (! [UIImagePickerController isFlashAvailableForCameraDevice:UIImagePickerControllerCameraDeviceRear ]) 
    return; 

if (PickerController.cameraFlashMode == UIImagePickerControllerCameraFlashModeOff) 
    PickerController.cameraFlashMode = UIImagePickerControllerCameraFlashModeOn; 
else 
    PickerController.cameraFlashMode = UIImagePickerControllerCameraFlashModeOff; 
} 
+0

非常感謝。 – Fattie 2014-02-28 19:26:41

0
// not all devices have two cameras or a flash so just check here 
    if ([UIImagePickerController isCameraDeviceAvailable: UIImagePickerControllerCameraDeviceRear]) { 
     imagePicker.cameraDevice = UIImagePickerControllerCameraDeviceRear; 
     if ([UIImagePickerController isCameraDeviceAvailable: UIImagePickerControllerCameraDeviceFront]) { 
      cameraSelectionButton.alpha = 1.0; 
      showCameraSelection = YES; 
     } 
    } else { 
     imagePicker.cameraDevice = UIImagePickerControllerCameraDeviceFront; 
    } 

    // flash mode on 
     if ([UIImagePickerController isFlashAvailableForCameraDevice:imagePicker.cameraDevice]) 
     { 
      imagePicker.cameraFlashMode = UIImagePickerControllerCameraFlashModeOff; 
      flashModeButton.alpha = 1.0; 
      showFlashMode = YES; 
     }