12

我有 iPhone 应用程序,它用自定义视图覆盖相机。我有一个按钮可以在相机闪光模式之间切换,这是代码

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

这是我的问题:在 iOS 7.0x 上工作得非常好,但在 iOS 7.1 中,无论其真实状态如何,cameraFlashMode 属性都会返回 UIImagePickerControllerCameraFlashModeAuto。

闪光模式确实改变了,但我没有得到任何迹象。有什么线索吗?谢谢

4

4 回答 4

12

我是这样解决的:

@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;
于 2014-03-27T03:20:05.400 回答
5

好的,所以我对此进行了非常详细的研究,并在网上偶然发现了这篇有用的文章:

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

我接受了他们的建议,现在我在用户拍照之前设置了闪光灯设置。我没有检查相机当前的闪光灯设置是什么,而是检查我的闪光灯按钮的 titleLabel 文本以查看用户想要的闪光灯设置:

这是我想出的代码,它现在完美地解决了我的问题。我希望这可以帮助大家解决在 iOS 7.0 上不存在但现在在 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];
}
于 2014-03-23T02:37:18.760 回答
2

尝试使用 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];
    }
于 2014-03-26T15:46:39.483 回答
0

上面的答案在 iOS 7.1 中对我不起作用 @daidai 这就是我所做的,这对我有用

在您的 .h 文件中,属性 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);
    }
}
于 2014-08-02T11:59:58.727 回答