diff --git a/ios/RCTCameraManager.m b/ios/RCTCameraManager.m index 698e84b..5dd3767 100644 --- a/ios/RCTCameraManager.m +++ b/ios/RCTCameraManager.m @@ -158,7 +158,7 @@ RCT_EXPORT_VIEW_PROPERTY(onZoomChanged, BOOL) self.sessionQueue = dispatch_queue_create("cameraManagerQueue", DISPATCH_QUEUE_SERIAL); - + self.sensorOrientationChecker = [RCTSensorOrientationChecker new]; } return self; } @@ -465,7 +465,7 @@ RCT_EXPORT_METHOD(hasFlash:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRej NSData *imageData = UIImageJPEGRepresentation(image, 1.0); [self saveImage:imageData target:target metadata:nil resolve:resolve reject:reject]; #else - [self.sensorOrientationChecker getDeviceOrientation:^(UIInterfaceOrientation orientation) { + [self.sensorOrientationChecker getDeviceOrientationWithBlock:^(UIInterfaceOrientation orientation) { [[self.stillImageOutput connectionWithMediaType:AVMediaTypeVideo] setVideoOrientation:[self.sensorOrientationChecker convertToAVCaptureVideoOrientation: orientation]]; [self.stillImageOutput captureStillImageAsynchronouslyFromConnection:[self.stillImageOutput connectionWithMediaType:AVMediaTypeVideo] completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) { diff --git a/ios/RCTSensorOrientationChecker.h b/ios/RCTSensorOrientationChecker.h index 2de4447..fe0840e 100644 --- a/ios/RCTSensorOrientationChecker.h +++ b/ios/RCTSensorOrientationChecker.h @@ -9,9 +9,11 @@ #import #import +typedef void (^RCTSensorCallback) (UIInterfaceOrientation orientation); + @interface RCTSensorOrientationChecker : NSObject -- (void)getDeviceOrientation:(void(^)(UIInterfaceOrientation orientation))callback; +- (void)getDeviceOrientationWithBlock:(RCTSensorCallback)callback; - (AVCaptureVideoOrientation)convertToAVCaptureVideoOrientation:(UIInterfaceOrientation)orientation; @end diff --git a/ios/RCTSensorOrientationChecker.m b/ios/RCTSensorOrientationChecker.m index ba0a2a7..5437eac 100644 --- a/ios/RCTSensorOrientationChecker.m +++ b/ios/RCTSensorOrientationChecker.m @@ -9,38 +9,60 @@ #import "RCTSensorOrientationChecker.h" #import + @interface RCTSensorOrientationChecker () @property (strong, nonatomic) CMMotionManager * motionManager; +@property (assign, nonatomic) UIInterfaceOrientation orientation; +@property (strong, nonatomic) RCTSensorCallback orientationCallback; @end @implementation RCTSensorOrientationChecker -- (void)getDeviceOrientation:(void(^)(UIInterfaceOrientation orientation))callback +- (instancetype)init +{ + self = [super init]; + if (self) { + // Initialization code + self.motionManager = [[CMMotionManager alloc] init]; + self.motionManager.accelerometerUpdateInterval = 0.2; + self.motionManager.gyroUpdateInterval = 0.2; + self.orientationCallback = nil; + } + return self; +} + +- (void)resume { - self.motionManager = [[CMMotionManager alloc] init]; - self.motionManager.accelerometerUpdateInterval = DBL_MAX; // infinite delay, never update - self.motionManager.gyroUpdateInterval = DBL_MAX; - - __weak CMMotionManager * weakMotionManager = self.motionManager; __weak __typeof(self) weakSelf = self; - [self.motionManager startAccelerometerUpdatesToQueue:[NSOperationQueue new] withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) { - // stop after the first sample - [weakMotionManager stopAccelerometerUpdates]; - - UIInterfaceOrientation deviceOrientation = UIInterfaceOrientationUnknown; if (!error) { - deviceOrientation = [weakSelf getOrientationBy:accelerometerData.acceleration]; + self.orientation = [weakSelf getOrientationBy:accelerometerData.acceleration]; } - - if (callback) { - callback(deviceOrientation); + if (self.orientationCallback) { + self.orientationCallback(self.orientation); } }]; - +} + +- (void)pause +{ + [self.motionManager stopAccelerometerUpdates]; +} + +- (void)getDeviceOrientationWithBlock:(RCTSensorCallback)callback +{ + __weak __typeof(self) weakSelf = self; + self.orientationCallback = ^(UIInterfaceOrientation orientation) { + if (callback) { + callback(orientation); + } + weakSelf.orientationCallback = nil; + [weakSelf pause]; + }; + [self resume]; } - (UIInterfaceOrientation)getOrientationBy:(CMAcceleration)acceleration