From f7524c20ce4c3495fe6a660ce8191bcfd6820eb2 Mon Sep 17 00:00:00 2001 From: Radu Popovici Date: Thu, 24 Mar 2016 09:31:12 +0200 Subject: [PATCH] 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