停止手电筒ios

时间:2015-02-06 09:28:08

标签: ios objective-c nstimer flashlight

我有一个手电筒应用程序。 我用这个来制作flash:

NSTimer * myTimer = [NSTimer scheduledTimerWithTimeInterval:0.05 
                                                     target:self 
                                                   selector:@selector(toggleFlashlight) 
                                                   userInfo:nil 
                                                    repeats:YES];`
- (void)toggleFlashlight{


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

            [device lockForConfiguration:nil];
            if (device.torchMode == AVCaptureTorchModeOff) {
                [device setTorchMode:AVCaptureTorchModeOn];
                [device setFlashMode:AVCaptureFlashModeOn];
                //torchIsOn = YES;
            } else {
                [device setTorchMode:AVCaptureTorchModeOff];
                [device setFlashMode:AVCaptureFlashModeOff];
                // torchIsOn = NO;
            }

        [device unlockForConfiguration];
        }
    }    
}

但我想在按下按钮时停止它。我怎样才能做到这一点?如果我没有闪光灯,它正在努力阻止它。

1 个答案:

答案 0 :(得分:0)

在你停止方法中(除非你有闪光灯,否则你认为有效),取消任何预定的闪光定时器...并将相机设置为手电筒模式

//stop timer
[myTimer invalidate];

//unflash
Class captureDeviceClass = NSClassFromString(@"AVCaptureDevice");
if (captureDeviceClass != nil) {
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    if ([device hasTorch] && [device hasFlash]){
        [device lockForConfiguration:nil];
        [device setTorchMode:AVCaptureTorchModeOff];
        [device setFlashMode:AVCaptureFlashModeOff];
    }
    [device unlockForConfiguration];
}