iOS 7.1 imagePicker CameraFlashMode not indicating Flash state

北城余情 提交于 2019-12-04 02:10:57

I solved it like this:

@property (nonatomic) NSInteger flashMode;

if (_flashMode == UIImagePickerControllerCameraFlashModeAuto)
{
    _flashMode = UIImagePickerControllerCameraFlashModeOff;
}
else if (_flashMode == UIImagePickerControllerCameraFlashModeOff)
{
    _flashMode = UIImagePickerControllerCameraFlashModeOn;
}
else if (_flashMode == UIImagePickerControllerCameraFlashModeOn)
{
    _flashMode = UIImagePickerControllerCameraFlashModeAuto;
}

_cameraPicker.cameraFlashMode = (UIImagePickerControllerCameraFlashMode)_flashMode;

Okay, so I researched this in great detail, and stumbled upon this helpful article online:

http://www.c2itconsulting.com/2013/10/ios-flash-setting-on-camera-picker-only-available-after-view-is-displayed/

I took their advice, and now I set the flash setting just before the user takes the picture. Instead of checking to see what the camera's current flash setting is, all I do is check my flash button's titleLabel text to see what the user wants as their flash setting:

Here is the code I came up with, which solves the problem perfectly for me now. I hope this helps out all of you with this same problem that didn't exist on iOS 7.0, but now does on iOS 7.1.

#define deviceHasCameraFlash [UIImagePickerController isFlashAvailableForCameraDevice:UIImagePickerControllerCameraDeviceRear]

- (void)capturePhoto
{
    if (self.cameraDevice != UIImagePickerControllerCameraDeviceFront && deviceHasCameraFlash)
    {
        if ([self.flashButton.titleLabel.text isEqualToString:@"Auto"])
        {
            self.cameraFlashMode = UIImagePickerControllerCameraFlashModeAuto;
        }
        else if ([self.flashButton.titleLabel.text isEqualToString:@"Off"])
        {
            self.cameraFlashMode = UIImagePickerControllerCameraFlashModeOff;
        }
        else if ([self.flashButton.titleLabel.text isEqualToString:@"On"])
        {
            self.cameraFlashMode = UIImagePickerControllerCameraFlashModeOn;
        }
    }

   [self takePicture];
}

Try to use AVCaptureDevice:

    Class captureDeviceClass = NSClassFromString(@"AVCaptureDevice");

    if (captureDeviceClass != nil) {
        AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

        [device lockForConfiguration:nil];

        if ([device hasTorch]) {
          if (device.torchMode ==  AVCaptureTorchModeAuto) {
            NSLog(@"Auto");
          }
          if (device.torchMode ==  AVCaptureTorchModeOn) {
            NSLog(@"On");
          }
          if (device.torchMode ==  AVCaptureTorchModeOff) {
            NSLog(@"Of");
          }
        }

        if ([device hasFlash]) {
          if (device.flashMode ==  AVCaptureFlashModeAuto) {
            NSLog(@"Auto");
          }
          if (device.flashMode ==  AVCaptureFlashModeOn) {
            NSLog(@"On");
          }
          if (device.flashMode ==  AVCaptureFlashModeOff) {
            NSLog(@"Of");
          }
        }
        [device unlockForConfiguration];
    }

The answer above did't worked for me in iOS 7.1 @daidai this is what i did and this worked for me

In your .h-file the property flashMode

- (void)didTapFlash:(id)sender
{



    if (self.flashMode == UIImagePickerControllerCameraFlashModeAuto) {
        //toggle your button to "on"
        [self.imagePicker setCameraFlashMode:UIImagePickerControllerCameraFlashModeOn];
        [self.flashButton setImage:[UIImage imageNamed:@"flashOn"] forState:UIControlStateNormal];
         self.flashMode = UIImagePickerControllerCameraFlashModeOn;

        NSLog(@"On state: %d", self.flashMode);
    }else if (self.flashMode == UIImagePickerControllerCameraFlashModeOn){
        //toggle your button to "Off"
        [self.imagePicker setCameraFlashMode:UIImagePickerControllerCameraFlashModeOff];
        [self.flashButton setImage:[UIImage imageNamed:@"flashOf"] forState:UIControlStateNormal];
        self.flashMode = UIImagePickerControllerCameraFlashModeOff;

        NSLog(@"Of state: %d", self.flashMode);
    }else if (self.flashMode == UIImagePickerControllerCameraFlashModeOff){
        //toggle your button to "Auto"
        [self.imagePicker setCameraFlashMode:UIImagePickerControllerCameraFlashModeAuto];
        [self.flashButton setImage:[UIImage imageNamed:@"flashAuto"] forState:UIControlStateNormal];
         self.flashMode = UIImagePickerControllerCameraFlashModeAuto;

        NSLog(@"Auto state: %d", self.flashMode);
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!