1

我需要在我的应用程序中使用 iPhone 闪光灯。但是,当用户打开闪光灯时,相机不会拍照。我怎样才能做到这一点?在这里,我附上了我的代码。但是,当我打开闪光灯时,相机会拍照。

AVCaptureDeviceInput *flashInput = [AVCaptureDeviceInput deviceInputWithDevice:device error: nil];
            AVCaptureVideoDataOutput *output = [[AVCaptureVideoDataOutput alloc] init];

            AVCaptureSession *session = [[AVCaptureSession alloc] init];

            [session beginConfiguration];
            [device lockForConfiguration:nil];

            [device setTorchMode:AVCaptureTorchModeOn];
            [device setFlashMode:AVCaptureFlashModeOn];

            [session addInput:flashInput];
            [session addOutput:output];

            [device unlockForConfiguration];

            [output release];

            [session commitConfiguration];
            [session startRunning];

            [self setTorchSession:session];

我在哪里编码错了?请帮我。提前致谢。

4

2 回答 2

5

我的应用程序中有一个手电筒按钮,它使用以下 3 种方法。

- (void)initialiseTorch {

    if (!session) {
        AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
        session = [[AVCaptureSession alloc] init];
        AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error: nil];
        [session addInput:input];
        AVCaptureVideoDataOutput *output = [[AVCaptureVideoDataOutput alloc] init];
        [session addOutput:output];
        [session startRunning];
        [output release];
    }
}

- (void)releaseTorch {  
    if (session) {
        [session stopRunning];
        [session release];
        session = nil;
    }
}

- (void) lightButtonPressed {    

    if (!session) {
        [self initialiseTorch];
    }

    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

    [session beginConfiguration];
    [device lockForConfiguration:nil];
    if ([device torchMode] == AVCaptureTorchModeOn) {
        [device setTorchMode:AVCaptureTorchModeOff];
    } else {
        [device setTorchMode:AVCaptureTorchModeOn];
    }
    [device unlockForConfiguration];
    [session commitConfiguration];
}

我可以看到我们的代码之间的唯一区别是您还设置了 Flash 模式。我还配置了我的会话,然后在单独的 beginConfiguration 通道中打开/关闭手电筒

于 2011-12-08T09:42:35.667 回答
0

检查这个...

- (void)torchOnOff: (BOOL) onOff
{
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    if ([device hasTorch]) {
        [device lockForConfiguration:nil];
        [device setTorchMode: onOff ? AVCaptureTorchModeOn : AVCaptureTorchModeOff];
        [device unlockForConfiguration];
    }
}
于 2013-05-27T11:50:14.147 回答