2

我有以下代码来打开手电筒和闪光灯:

                AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
            if ([device hasTorch]) {
                [self.session beginConfiguration];
                [device lockForConfiguration:nil];
                if(self.flashlightOn == YES)
                {
                    [device setTorchMode:AVCaptureTorchModeOn];
                    [device setFlashMode:AVCaptureFlashModeOn];
                    NSLog(@"TurnFlashOn");
                }
                else
                {
                    [device setTorchMode:AVCaptureTorchModeOff];
                    [device setFlashMode:AVCaptureFlashModeOff];
                    NSLog(@"TurnFlashOff");
                }

                [device unlockForConfiguration];
                [self.session commitConfiguration];

但是,如果调用代码我的会话似乎冻结,如果我删除 [device setTorchMode:] 部分一切正常。有人知道这里发生了什么吗?

编辑:它仅在没有 IDE 的情况下运行应用程序时出现 -> 单独运行

4

1 回答 1

3

我在前后摄像头之间交换时遇到了同样的问题。我第一次使用该设备时它会起作用,然后每次都冻结在“commitConfiguration”上。似乎正确的顺序是:

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

            //*** Do your session configuration stuff here

            [device unlockForConfiguration];
            [self.session commitConfiguration];

            //*** And this is what fixed the problem:
            self.session startRunning];

希望这对你有用!

于 2013-01-17T13:21:20.557 回答