refactor sensor orientation checker
This commit is contained in:
parent
f7524c20ce
commit
379f75d7a6
|
@ -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) {
|
||||
|
|
|
@ -9,9 +9,11 @@
|
|||
#import <UIKit/UIKit.h>
|
||||
#import <AVFoundation/AVFoundation.h>
|
||||
|
||||
typedef void (^RCTSensorCallback) (UIInterfaceOrientation orientation);
|
||||
|
||||
@interface RCTSensorOrientationChecker : NSObject
|
||||
|
||||
- (void)getDeviceOrientation:(void(^)(UIInterfaceOrientation orientation))callback;
|
||||
- (void)getDeviceOrientationWithBlock:(RCTSensorCallback)callback;
|
||||
- (AVCaptureVideoOrientation)convertToAVCaptureVideoOrientation:(UIInterfaceOrientation)orientation;
|
||||
|
||||
@end
|
||||
|
|
|
@ -9,38 +9,60 @@
|
|||
#import "RCTSensorOrientationChecker.h"
|
||||
#import <CoreMotion/CoreMotion.h>
|
||||
|
||||
|
||||
@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
|
||||
|
|
Loading…
Reference in New Issue