How to access camera flash on UIImagePickerController?

前端 未结 3 2173
醉话见心
醉话见心 2021-02-09 17:43

I would like to know how to switch on the camera flash on the iPhone 4 with UIImagePickerController.

if ([UIImagePickerController isSourceTypeAvailable:UIImagePi         


        
相关标签:
3条回答
  • 2021-02-09 18:15
    -(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;  
    }
    
    0 讨论(0)
  • 2021-02-09 18:18
    // 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;
            }
    
    0 讨论(0)
  • 2021-02-09 18:29

    You can use this. Basically call 'toggleTorch' when you want to turn the flash on or off. 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];
    }
    
    0 讨论(0)
提交回复
热议问题