iOS 7.1 imagePicker CameraFlashMode not indicating Flash state

强颜欢笑 提交于 2020-01-01 08:24:09

问题


I have iPhone application which overlays the camera with custom view. I have a button to switch between camera flash mode, this is the code

switch ([self.imagePickerController cameraFlashMode]) {
    case UIImagePickerControllerCameraFlashModeAuto:
        [self.imagePickerController setCameraFlashMode:UIImagePickerControllerCameraFlashModeOn];
        return @"On";
        break;

    case UIImagePickerControllerCameraFlashModeOn:
        [self.imagePickerController setCameraFlashMode:UIImagePickerControllerCameraFlashModeOff];
        return @"Off";
        break;

    case UIImagePickerControllerCameraFlashModeOff:
        [self.imagePickerController setCameraFlashMode:UIImagePickerControllerCameraFlashModeAuto];
        return @"Auto";
        break;

    default:
        break;
}

This is my problem: Worked perfectly fine on iOS 7.0x, but in iOS 7.1 the cameraFlashMode property returns UIImagePickerControllerCameraFlashModeAuto regardless of its real state.

The flash mode does change, but i get no indication of that. Any clues? Thanks


回答1:


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;



回答2:


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



回答3:


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



回答4:


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


来源:https://stackoverflow.com/questions/22457097/ios-7-1-imagepicker-cameraflashmode-not-indicating-flash-state

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!