سؤال

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

هل كانت مفيدة؟

المحلول

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);
    }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top