From f3defa5ed54930c1c8ef3d722235a84429441d94 Mon Sep 17 00:00:00 2001 From: changqing Date: Wed, 24 Oct 2018 13:22:25 +0800 Subject: [PATCH] add startFlash,stopFlash like android --- ios/RCTBarcode/RCTBarcode/RCTBarcodeManager.m | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/ios/RCTBarcode/RCTBarcode/RCTBarcodeManager.m b/ios/RCTBarcode/RCTBarcode/RCTBarcodeManager.m index e871da8..01aee8e 100644 --- a/ios/RCTBarcode/RCTBarcode/RCTBarcodeManager.m +++ b/ios/RCTBarcode/RCTBarcode/RCTBarcodeManager.m @@ -267,6 +267,73 @@ - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects: +RCT_EXPORT_METHOD(startFlash) { +#if TARGET_IPHONE_SIMULATOR + return; +#endif + dispatch_async(dispatch_get_main_queue(), ^{ + + AVCaptureDevice *device = [self.videoCaptureDeviceInput device]; + + NSError *error = nil; + + if (![device hasFlash]) return; + if (![device lockForConfiguration:&error]) { + NSLog(@"%@", error); + return; + } + if (device.hasFlash ) + { + NSError *error = nil; + if ([device lockForConfiguration:&error]) + { + [device setTorchMode:AVCaptureFlashModeOn]; + [device unlockForConfiguration]; + } + else + { + NSLog(@"%@", error); + } + } + [device unlockForConfiguration]; + + + }); +} + +RCT_EXPORT_METHOD(stopFlash) { +#if TARGET_IPHONE_SIMULATOR + return; +#endif + dispatch_async(dispatch_get_main_queue(), ^{ + + AVCaptureDevice *device = [self.videoCaptureDeviceInput device]; + + NSError *error = nil; + + if (![device hasFlash]) return; + if (![device lockForConfiguration:&error]) { + NSLog(@"%@", error); + return; + } + if (device.hasFlash ) + { + NSError *error = nil; + if ([device lockForConfiguration:&error]) + { + [device setTorchMode:AVCaptureFlashModeOff]; + [device unlockForConfiguration]; + } + else + { + NSLog(@"%@", error); + } + } + [device unlockForConfiguration]; + + + }); +} - (NSDictionary *)constantsToExport