From b0c67ba556ccc1bbacafa8ca362f3f51178c8406 Mon Sep 17 00:00:00 2001 From: Yasuyuki Toriumi mac Date: Tue, 29 Dec 2015 18:08:47 +0900 Subject: [PATCH 1/5] fix iOS snapshot orientation when device orientation is locked --- ios/RCTCameraManager.m | 70 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 69 insertions(+), 1 deletion(-) diff --git a/ios/RCTCameraManager.m b/ios/RCTCameraManager.m index 2e6423a..fe87ff1 100644 --- a/ios/RCTCameraManager.m +++ b/ios/RCTCameraManager.m @@ -9,6 +9,13 @@ #import #import #import +#import + +@interface RCTCameraManager () + +@property (strong, nonatomic) CMMotionManager * motionManager; + +@end @implementation RCTCameraManager @@ -458,7 +465,8 @@ 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.stillImageOutput connectionWithMediaType:AVMediaTypeVideo] setVideoOrientation:self.previewLayer.connection.videoOrientation]; + [self getDeviceOrientation:^(UIInterfaceOrientation orientation) { + [[self.stillImageOutput connectionWithMediaType:AVMediaTypeVideo] setVideoOrientation:[self convertToAVCaptureVideoOrientation: orientation]]; [self.stillImageOutput captureStillImageAsynchronouslyFromConnection:[self.stillImageOutput connectionWithMediaType:AVMediaTypeVideo] completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) { @@ -521,6 +529,7 @@ RCT_EXPORT_METHOD(hasFlash:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRej reject(RCTErrorUnspecified, nil, RCTErrorWithMessage(error.description)); } }]; + }]; #endif }); } @@ -863,4 +872,63 @@ didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL } } +- (void)getDeviceOrientation:(void(^)(UIInterfaceOrientation orientation))callback +{ + 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]; + } + + if (callback) { + callback(deviceOrientation); + } + }]; + +} + +- (UIInterfaceOrientation)getOrientationBy:(CMAcceleration)acceleration +{ + if(acceleration.x >= 0.75) { + return UIInterfaceOrientationLandscapeLeft; + } + if(acceleration.x <= -0.75) { + return UIInterfaceOrientationLandscapeRight; + } + if(acceleration.y >= -0.75) { + return UIInterfaceOrientationPortrait; + } + if(acceleration.y >= 0.75) { + return UIInterfaceOrientationPortraitUpsideDown; + } + return [[UIApplication sharedApplication] statusBarOrientation]; +} + +- (AVCaptureVideoOrientation)convertToAVCaptureVideoOrientation:(UIInterfaceOrientation)orientation +{ + switch (orientation) { + case UIInterfaceOrientationPortrait: + return AVCaptureVideoOrientationPortrait; + case UIInterfaceOrientationPortraitUpsideDown: + return AVCaptureVideoOrientationPortraitUpsideDown; + case UIInterfaceOrientationLandscapeLeft: + return AVCaptureVideoOrientationLandscapeLeft; + case UIInterfaceOrientationLandscapeRight: + return AVCaptureVideoOrientationLandscapeRight; + default: + return 0; // unknown + } +} + @end From 4ab7a9d220f8f48809f0403e004b817b5649f064 Mon Sep 17 00:00:00 2001 From: Radu Popovici Date: Wed, 23 Mar 2016 14:16:39 +0200 Subject: [PATCH 2/5] fix android snapshot orientation when device orientation is locked --- .../com/lwansbrough/RCTCamera/RCTCamera.java | 26 ++++++ .../RCTCamera/RCTCameraModule.java | 16 ++++ .../RCTSensorOrientationChecker.java | 84 +++++++++++++++++++ 3 files changed, 126 insertions(+) create mode 100644 android/src/main/java/com/lwansbrough/RCTCamera/RCTSensorOrientationChecker.java diff --git a/android/src/main/java/com/lwansbrough/RCTCamera/RCTCamera.java b/android/src/main/java/com/lwansbrough/RCTCamera/RCTCamera.java index 08d9260..44322e3 100644 --- a/android/src/main/java/com/lwansbrough/RCTCamera/RCTCamera.java +++ b/android/src/main/java/com/lwansbrough/RCTCamera/RCTCamera.java @@ -224,6 +224,32 @@ public class RCTCamera { } } + public void adjustCameraRotationToDeviceOrientation(int type, int deviceOrientation) + { + Camera camera = _cameras.get(type); + if (null == camera) { + return; + } + + CameraInfoWrapper cameraInfo = _cameraInfos.get(type); + int rotation; + int orientation = cameraInfo.info.orientation; + if (cameraInfo.info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) { + rotation = (orientation + deviceOrientation * 90) % 360; + } else { + rotation = (orientation - deviceOrientation * 90 + 360) % 360; + } + cameraInfo.rotation = rotation; + Camera.Parameters parameters = camera.getParameters(); + parameters.setRotation(cameraInfo.rotation); + + try { + camera.setParameters(parameters); + } catch (Exception e) { + e.printStackTrace(); + } + } + private void adjustPreviewLayout(int type) { Camera camera = _cameras.get(type); if (null == camera) { diff --git a/android/src/main/java/com/lwansbrough/RCTCamera/RCTCameraModule.java b/android/src/main/java/com/lwansbrough/RCTCamera/RCTCameraModule.java index b73e5d1..ea3585d 100644 --- a/android/src/main/java/com/lwansbrough/RCTCamera/RCTCameraModule.java +++ b/android/src/main/java/com/lwansbrough/RCTCamera/RCTCameraModule.java @@ -51,10 +51,12 @@ public class RCTCameraModule extends ReactContextBaseJavaModule { public static final int MEDIA_TYPE_VIDEO = 2; private final ReactApplicationContext _reactContext; + private RCTSensorOrientationChecker _sensorOrientationChecker; public RCTCameraModule(ReactApplicationContext reactContext) { super(reactContext); _reactContext = reactContext; + _sensorOrientationChecker = new RCTSensorOrientationChecker(_reactContext); } @Override @@ -162,11 +164,25 @@ public class RCTCameraModule extends ReactContextBaseJavaModule { @ReactMethod public void capture(final ReadableMap options, final Promise promise) { + _sensorOrientationChecker.onResume(); + _sensorOrientationChecker.registerOrientationListener(new RCTSensorOrientationListener() { + @Override + public void orientationEvent() { + int deviceOrientation = _sensorOrientationChecker.getOrientation(); + _sensorOrientationChecker.unregisterOrientationListener(); + _sensorOrientationChecker.onPause(); + captureWithOrientation(options, promise, deviceOrientation); + } + }); + } + + public void captureWithOrientation(final ReadableMap options, final Promise promise, int deviceOrientation) { Camera camera = RCTCamera.getInstance().acquireCameraInstance(options.getInt("type")); if (null == camera) { promise.reject("No camera found."); return; } + RCTCamera.getInstance().adjustCameraRotationToDeviceOrientation(options.getInt("type"), deviceOrientation); RCTCamera.getInstance().setCaptureQuality(options.getInt("type"), options.getString("quality")); camera.takePicture(null, null, new Camera.PictureCallback() { @Override diff --git a/android/src/main/java/com/lwansbrough/RCTCamera/RCTSensorOrientationChecker.java b/android/src/main/java/com/lwansbrough/RCTCamera/RCTSensorOrientationChecker.java new file mode 100644 index 0000000..c2aadfb --- /dev/null +++ b/android/src/main/java/com/lwansbrough/RCTCamera/RCTSensorOrientationChecker.java @@ -0,0 +1,84 @@ +/** + * Created by rpopovici on 23/03/16. + */ + +package com.lwansbrough.RCTCamera; + +import android.content.Context; +import android.hardware.Sensor; +import android.hardware.SensorEvent; +import android.hardware.SensorEventListener; +import android.hardware.SensorManager; + +import com.facebook.react.bridge.ReactApplicationContext; + +interface RCTSensorOrientationListener { + void orientationEvent(); +} + +public class RCTSensorOrientationChecker { + + int mOrientation = 0; + private SensorEventListener mSensorEventListener; + private SensorManager mSensorManager; + private RCTSensorOrientationListener mListener = null; + + public RCTSensorOrientationChecker( ReactApplicationContext reactContext) { + mSensorEventListener = new Listener(); + mSensorManager = (SensorManager) reactContext.getSystemService(Context.SENSOR_SERVICE); + + } + + /** + * Call on activity onResume() + */ + public void onResume() { + mSensorManager.registerListener(mSensorEventListener, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL); + } + + /** + * Call on activity onPause() + */ + public void onPause() { + mSensorManager.unregisterListener(mSensorEventListener); + } + + private class Listener implements SensorEventListener { + + @Override + public void onSensorChanged(SensorEvent event) { + float x = event.values[0]; + float y = event.values[1]; + + if (x<5 && x>-5 && y > 5) + mOrientation = 0; + else if (x<-5 && y<5 && y>-5) + mOrientation = 3; + else if (x<5 && x>-5 && y<-5) + mOrientation = 2; + else if (x>5 && y<5 && y>-5) + mOrientation = 1; + + if (mListener != null) { + mListener.orientationEvent(); + } + } + + @Override + public void onAccuracyChanged(Sensor sensor, int accuracy) { + + } + } + + public int getOrientation() { + return mOrientation; + } + + public void registerOrientationListener(RCTSensorOrientationListener listener) { + this.mListener = listener; + } + + public void unregisterOrientationListener() { + mListener = null; + } +} From f7524c20ce4c3495fe6a660ce8191bcfd6820eb2 Mon Sep 17 00:00:00 2001 From: Radu Popovici Date: Thu, 24 Mar 2016 09:31:12 +0200 Subject: [PATCH 3/5] extract sensor checker to a separate class --- ios/RCTCamera.xcodeproj/project.pbxproj | 6 ++ ios/RCTCameraManager.m | 67 ++------------------- ios/RCTSensorOrientationChecker.h | 17 ++++++ ios/RCTSensorOrientationChecker.m | 79 +++++++++++++++++++++++++ 4 files changed, 106 insertions(+), 63 deletions(-) create mode 100644 ios/RCTSensorOrientationChecker.h create mode 100644 ios/RCTSensorOrientationChecker.m diff --git a/ios/RCTCamera.xcodeproj/project.pbxproj b/ios/RCTCamera.xcodeproj/project.pbxproj index eda5d03..e623715 100644 --- a/ios/RCTCamera.xcodeproj/project.pbxproj +++ b/ios/RCTCamera.xcodeproj/project.pbxproj @@ -11,6 +11,7 @@ 4107014D1ACB732B00C6AA39 /* RCTCamera.m in Sources */ = {isa = PBXBuildFile; fileRef = 410701481ACB732B00C6AA39 /* RCTCamera.m */; }; 4107014E1ACB732B00C6AA39 /* RCTCameraManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 4107014A1ACB732B00C6AA39 /* RCTCameraManager.m */; }; 454EBCF41B5082DC00AD0F86 /* NSMutableDictionary+ImageMetadata.m in Sources */ = {isa = PBXBuildFile; fileRef = 454EBCF31B5082DC00AD0F86 /* NSMutableDictionary+ImageMetadata.m */; }; + 9FE592B31CA3CBF500788287 /* RCTSensorOrientationChecker.m in Sources */ = {isa = PBXBuildFile; fileRef = 9FE592B21CA3CBF500788287 /* RCTSensorOrientationChecker.m */; }; /* End PBXBuildFile section */ /* Begin PBXCopyFilesBuildPhase section */ @@ -34,6 +35,8 @@ 410701491ACB732B00C6AA39 /* RCTCameraManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RCTCameraManager.h; sourceTree = ""; }; 4107014A1ACB732B00C6AA39 /* RCTCameraManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTCameraManager.m; sourceTree = ""; }; 454EBCF31B5082DC00AD0F86 /* NSMutableDictionary+ImageMetadata.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSMutableDictionary+ImageMetadata.m"; sourceTree = ""; }; + 9FE592B11CA3CBF500788287 /* RCTSensorOrientationChecker.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RCTSensorOrientationChecker.h; sourceTree = ""; }; + 9FE592B21CA3CBF500788287 /* RCTSensorOrientationChecker.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTSensorOrientationChecker.m; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -50,6 +53,8 @@ 410701241ACB719800C6AA39 = { isa = PBXGroup; children = ( + 9FE592B11CA3CBF500788287 /* RCTSensorOrientationChecker.h */, + 9FE592B21CA3CBF500788287 /* RCTSensorOrientationChecker.m */, 0314E39C1B661A460092D183 /* CameraFocusSquare.m */, 0314E39B1B661A0C0092D183 /* CameraFocusSquare.h */, 454EBCF31B5082DC00AD0F86 /* NSMutableDictionary+ImageMetadata.m */, @@ -128,6 +133,7 @@ 454EBCF41B5082DC00AD0F86 /* NSMutableDictionary+ImageMetadata.m in Sources */, 4107014E1ACB732B00C6AA39 /* RCTCameraManager.m in Sources */, 4107014D1ACB732B00C6AA39 /* RCTCamera.m in Sources */, + 9FE592B31CA3CBF500788287 /* RCTSensorOrientationChecker.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/ios/RCTCameraManager.m b/ios/RCTCameraManager.m index fe87ff1..698e84b 100644 --- a/ios/RCTCameraManager.m +++ b/ios/RCTCameraManager.m @@ -9,11 +9,11 @@ #import #import #import -#import +#import "RCTSensorOrientationChecker.h" @interface RCTCameraManager () -@property (strong, nonatomic) CMMotionManager * motionManager; +@property (strong, nonatomic) RCTSensorOrientationChecker * sensorOrientationChecker; @end @@ -465,8 +465,8 @@ 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 getDeviceOrientation:^(UIInterfaceOrientation orientation) { - [[self.stillImageOutput connectionWithMediaType:AVMediaTypeVideo] setVideoOrientation:[self convertToAVCaptureVideoOrientation: orientation]]; + [self.sensorOrientationChecker getDeviceOrientation:^(UIInterfaceOrientation orientation) { + [[self.stillImageOutput connectionWithMediaType:AVMediaTypeVideo] setVideoOrientation:[self.sensorOrientationChecker convertToAVCaptureVideoOrientation: orientation]]; [self.stillImageOutput captureStillImageAsynchronouslyFromConnection:[self.stillImageOutput connectionWithMediaType:AVMediaTypeVideo] completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) { @@ -872,63 +872,4 @@ didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL } } -- (void)getDeviceOrientation:(void(^)(UIInterfaceOrientation orientation))callback -{ - 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]; - } - - if (callback) { - callback(deviceOrientation); - } - }]; - -} - -- (UIInterfaceOrientation)getOrientationBy:(CMAcceleration)acceleration -{ - if(acceleration.x >= 0.75) { - return UIInterfaceOrientationLandscapeLeft; - } - if(acceleration.x <= -0.75) { - return UIInterfaceOrientationLandscapeRight; - } - if(acceleration.y >= -0.75) { - return UIInterfaceOrientationPortrait; - } - if(acceleration.y >= 0.75) { - return UIInterfaceOrientationPortraitUpsideDown; - } - return [[UIApplication sharedApplication] statusBarOrientation]; -} - -- (AVCaptureVideoOrientation)convertToAVCaptureVideoOrientation:(UIInterfaceOrientation)orientation -{ - switch (orientation) { - case UIInterfaceOrientationPortrait: - return AVCaptureVideoOrientationPortrait; - case UIInterfaceOrientationPortraitUpsideDown: - return AVCaptureVideoOrientationPortraitUpsideDown; - case UIInterfaceOrientationLandscapeLeft: - return AVCaptureVideoOrientationLandscapeLeft; - case UIInterfaceOrientationLandscapeRight: - return AVCaptureVideoOrientationLandscapeRight; - default: - return 0; // unknown - } -} - @end diff --git a/ios/RCTSensorOrientationChecker.h b/ios/RCTSensorOrientationChecker.h new file mode 100644 index 0000000..2de4447 --- /dev/null +++ b/ios/RCTSensorOrientationChecker.h @@ -0,0 +1,17 @@ +// +// RCTSensorOrientationChecker.h +// RCTCamera +// +// Created by Radu Popovici on 24/03/16. +// +// + +#import +#import + +@interface RCTSensorOrientationChecker : NSObject + +- (void)getDeviceOrientation:(void(^)(UIInterfaceOrientation orientation))callback; +- (AVCaptureVideoOrientation)convertToAVCaptureVideoOrientation:(UIInterfaceOrientation)orientation; + +@end diff --git a/ios/RCTSensorOrientationChecker.m b/ios/RCTSensorOrientationChecker.m new file mode 100644 index 0000000..ba0a2a7 --- /dev/null +++ b/ios/RCTSensorOrientationChecker.m @@ -0,0 +1,79 @@ +// +// RCTSensorOrientationChecker.m +// RCTCamera +// +// Created by Radu Popovici on 24/03/16. +// +// + +#import "RCTSensorOrientationChecker.h" +#import + +@interface RCTSensorOrientationChecker () + +@property (strong, nonatomic) CMMotionManager * motionManager; + +@end + +@implementation RCTSensorOrientationChecker + +- (void)getDeviceOrientation:(void(^)(UIInterfaceOrientation orientation))callback +{ + 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]; + } + + if (callback) { + callback(deviceOrientation); + } + }]; + +} + +- (UIInterfaceOrientation)getOrientationBy:(CMAcceleration)acceleration +{ + if(acceleration.x >= 0.75) { + return UIInterfaceOrientationLandscapeLeft; + } + if(acceleration.x <= -0.75) { + return UIInterfaceOrientationLandscapeRight; + } + if(acceleration.y >= -0.75) { + return UIInterfaceOrientationPortrait; + } + if(acceleration.y >= 0.75) { + return UIInterfaceOrientationPortraitUpsideDown; + } + return [[UIApplication sharedApplication] statusBarOrientation]; +} + +- (AVCaptureVideoOrientation)convertToAVCaptureVideoOrientation:(UIInterfaceOrientation)orientation +{ + switch (orientation) { + case UIInterfaceOrientationPortrait: + return AVCaptureVideoOrientationPortrait; + case UIInterfaceOrientationPortraitUpsideDown: + return AVCaptureVideoOrientationPortraitUpsideDown; + case UIInterfaceOrientationLandscapeLeft: + return AVCaptureVideoOrientationLandscapeLeft; + case UIInterfaceOrientationLandscapeRight: + return AVCaptureVideoOrientationLandscapeRight; + default: + return 0; // unknown + } +} + +@end From 379f75d7a6e397554f7283eebcdaa62683bd719a Mon Sep 17 00:00:00 2001 From: Radu Popovici Date: Thu, 24 Mar 2016 10:12:57 +0200 Subject: [PATCH 4/5] refactor sensor orientation checker --- ios/RCTCameraManager.m | 4 +-- ios/RCTSensorOrientationChecker.h | 4 ++- ios/RCTSensorOrientationChecker.m | 54 ++++++++++++++++++++++--------- 3 files changed, 43 insertions(+), 19 deletions(-) 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 From 5d98407e67ded0641e72b35dc7589d4b6885e8ef Mon Sep 17 00:00:00 2001 From: Radu Popovici Date: Thu, 24 Mar 2016 10:25:45 +0200 Subject: [PATCH 5/5] make orientation public --- ios/RCTSensorOrientationChecker.h | 2 ++ ios/RCTSensorOrientationChecker.m | 6 +++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/ios/RCTSensorOrientationChecker.h b/ios/RCTSensorOrientationChecker.h index fe0840e..15dc9e4 100644 --- a/ios/RCTSensorOrientationChecker.h +++ b/ios/RCTSensorOrientationChecker.h @@ -13,6 +13,8 @@ typedef void (^RCTSensorCallback) (UIInterfaceOrientation orientation); @interface RCTSensorOrientationChecker : NSObject +@property (assign, nonatomic) UIInterfaceOrientation orientation; + - (void)getDeviceOrientationWithBlock:(RCTSensorCallback)callback; - (AVCaptureVideoOrientation)convertToAVCaptureVideoOrientation:(UIInterfaceOrientation)orientation; diff --git a/ios/RCTSensorOrientationChecker.m b/ios/RCTSensorOrientationChecker.m index 5437eac..7957a13 100644 --- a/ios/RCTSensorOrientationChecker.m +++ b/ios/RCTSensorOrientationChecker.m @@ -13,7 +13,6 @@ @interface RCTSensorOrientationChecker () @property (strong, nonatomic) CMMotionManager * motionManager; -@property (assign, nonatomic) UIInterfaceOrientation orientation; @property (strong, nonatomic) RCTSensorCallback orientationCallback; @end @@ -33,6 +32,11 @@ return self; } +- (void)dealloc +{ + [self pause]; +} + - (void)resume { __weak __typeof(self) weakSelf = self;