Как получить доступ к вспышке камеры на UIImagePickerController? - PullRequest
5 голосов
/ 20 ноября 2010

Я хотел бы знать, как включить вспышку камеры на 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 Class Reference здесь: http://bit.ly/cdAhhB но я не нашел ответа.Может кто-нибудь помочь мне?

Спасибо

Ответы [ 3 ]

7 голосов
/ 05 февраля 2013
-(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;  
}
4 голосов
/ 20 ноября 2010

Вы можете использовать это. Обычно вызывайте 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:@"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];
}
0 голосов
/ 24 июня 2014
// 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;
        }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...