简体   繁体   English

如何在iPhone应用中使用手电筒?

[英]How to use torch light in iPhone app?

I need to use iPhone Flash light in my app. 我需要在我的应用中使用iPhone闪光灯。 But, while the user switch on the flash the camera does not take picture. 但是,当用户打开闪光灯时,相机不会拍照。 How can i do this? 我怎样才能做到这一点? Here i have attached my code. 在这里,我附上了我的代码。 But, when i switch on the flash light, the camera takes picture. 但是,当我打开闪光灯时,相机会拍照。

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

Where i am wrong in coding? 我在编码上哪里错了? Please help me. 请帮我。 Thanks in advance. 提前致谢。

I have a torch button in my app which uses the following 3 methods. 我的应用程序中有一个手电筒按钮,它使用以下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];
}

The only difference I can see between our code is that you are also setting the Flash Mode. 我在我们的代码之间看到的唯一区别是,您还设置了Flash模式。 Also I configure my session, and then turn the torch on/off in a seperate beginConfiguration pass 另外,我配置了会话,然后在单独的beginConfiguration传递中打开/关闭了割炬

check this... 检查这个...

- (void)torchOnOff: (BOOL) onOff
{
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    if ([device hasTorch]) {
        [device lockForConfiguration:nil];
        [device setTorchMode: onOff ? AVCaptureTorchModeOn : AVCaptureTorchModeOff];
        [device unlockForConfiguration];
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM