3

我需要使用前置摄像头并在 iPhone 中打开背光 LED。我怎样才能做到这一点?我可以使用以下代码打开前置摄像头:

- (void) turnCameraOn {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.delegate = self;
    imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    imagePicker.cameraDevice = UIImagePickerControllerCameraDeviceFront;
    imagePicker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
    imagePicker.showsCameraControls = YES;

    [self presentViewController:imagePicker animated:YES completion:nil];

} else {

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Camera unavaliable"
                                                    message:@"Unable to find camera on your device."
                                                   delegate:nil
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil, nil];
    [alert show];
    alert = nil;
}
}

我可以用这个代码打开led

- (void) turnTorchOn: (bool) on {
Class captureDeviceClass = NSClassFromString(@"AVCaptureDevice");
if (captureDeviceClass != nil) {
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    if ([device hasTorch] && [device hasFlash]){

        [device lockForConfiguration:nil];

        if (on) {
            [device setTorchMode:AVCaptureTorchModeOn];
            [device setFlashMode:AVCaptureFlashModeOn];
        } else {
            [device setTorchMode:AVCaptureTorchModeOff];
            [device setFlashMode:AVCaptureFlashModeOff];
        }
        [device unlockForConfiguration];
    }
}
}

但是当我打开APP并且前置摄像头出现时,LED熄灭。我需要同时工作。

谢谢

4

2 回答 2

0

这在具有当前 API 集的 iOS 上是不允许的。你可以通过苹果自己的相机应用程序来理解这一点。

当您切换到使用前置摄像头时,即使它们也会关闭手电筒,并且在该模式下不显示手电筒开关。

于 2015-02-03T14:34:58.500 回答
0

文档中检查isFlashAvailableForCameraDeviceUIImagePickerController 的属性以检查是否允许使用UIImagePickerControllerCameraDeviceFront属性进行闪存。

于 2015-02-03T15:10:01.110 回答