开辟设备闪关灯

- (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];
            torchIsOn = YES;
        } else {
            [device setTorchMode:AVCaptureTorchModeOff];
            [device setFlashMode:AVCaptureFlashModeOff];
            torchIsOn = NO;            
        }
        [device unlockForConfiguration];
    }
}
}
//该片段来自于http://outofmemory.cn
//点击的按钮时要执行的方法
-(void)ClickButton {  
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    if ([device hasTorch] && [device hasFlash]) {
        [device lockForConfiguration:nil];
        } 
    if (device.torchMode == AVCaptureTorchModeOn) {
        [device setTorchMode:AVCaptureTorchModeOff];     
    }else{
        [device setTorchMode:AVCaptureTorchModeOn]; 
    }
    [device unlockForConfiguration];
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图