2010-11-20 9 views
5

Me gustaría saber cómo encender el flash de la cámara en el iPhone 4 con UIImagePickerController.¿Cómo acceder al flash de la cámara en 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]; 
} 

}

ya leer la página web UIImagePickerController Referencia de las clases aquí: http://bit.ly/cdAhhB pero no encontré la respuesta. ¿Puede alguien ayudarme?

Gracias

+0

Esto no tiene nada que ver con Xcode. Voy a retomar esto. –

Respuesta

4

Puede usar esto. Básicamente llame a 'toggleTorch' cuando quiera encender o apagar el flash. Espero que esto sea lo que estabas buscando.

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

Voy a intentar eso. ¡Gracias! ; D – Aluminum

+3

esto es para la antorcha, y no tiene nada que ver con el flash. Simplemente cambie PickerController.cameraFlashMode para el flash. – Fattie

7
-(void)flashSelected 
{ 

if (PickerController.cameraFlashMode == 
UIImagePickerControllerCameraFlashModeOff) { 

    if ([UIImagePickerController 
isFlashAvailableForCameraDevice:UIImagePickerControllerCameraDeviceRear ]) 

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

alternativamente ..

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

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

gracias perfectos. – Fattie

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; 
     } 
Cuestiones relacionadas