简体   繁体   English

如何在UIImagePickerController上访问相机闪光灯?

[英]How to access camera flash on UIImagePickerController?

I would like to know how to switch on the camera flash on the iPhone 4 with UIImagePickerController. 我想知道如何使用UIImagePickerController打开iPhone 4上的相机闪光灯。

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

} }

I already read the UIImagePickerController Class Reference web page here: http://bit.ly/cdAhhB but I didn't find the answer. 我已经在这里阅读了UIImagePickerController类参考网页: http//bit.ly/cdAhhB但我找不到答案。 Can someone please help me? 有人可以帮帮我吗?

Thanks 谢谢

-(void)flashSelected
{

if (PickerController.cameraFlashMode == 
UIImagePickerControllerCameraFlashModeOff) {

    if ([UIImagePickerController 
isFlashAvailableForCameraDevice:UIImagePickerControllerCameraDeviceRear ])

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

alternately.. 交替..

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

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

You can use this. 你可以用它。 Basically call 'toggleTorch' when you want to turn the flash on or off. 当您想要打开或关闭闪光灯时,基本上可以调用'toggleTorch'。 Hopefully this is what you were looking for. 希望这是你想要的。

- (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:@"on@2x.png"] 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:@"off@2x.png"]];

            [torchSession stopRunning];

        }

    }

}

-(IBAction)powerBtn
{
    [self toggleTorch];
}
// 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;
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM