Skip to content

Commit

Permalink
fix orientation problem (#542)
Browse files Browse the repository at this point in the history
The captured content is not rotated as the device rotate, fixed.

Co-authored-by: Brett Wu (吴 书宇) <shuywu@tesla.com>
  • Loading branch information
summer-wu and Brett Wu (吴 书宇) committed May 22, 2023
1 parent 6196173 commit 0fb5003
Showing 1 changed file with 36 additions and 9 deletions.
45 changes: 36 additions & 9 deletions ios/ReactNativeCameraKit/CKCamera.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,20 @@
#import "CKCameraOverlayView.h"
#import "CKMockPreview.h"

AVCaptureVideoOrientation AVCaptureVideoOrientationFromInterfaceOrientation(UIInterfaceOrientation orientation){
if (orientation == UIInterfaceOrientationPortrait) {
return AVCaptureVideoOrientationPortrait;
} else if (orientation == UIInterfaceOrientationLandscapeLeft){
return AVCaptureVideoOrientationLandscapeLeft;
} else if (orientation == UIInterfaceOrientationLandscapeRight){
return AVCaptureVideoOrientationLandscapeRight;
} else if (orientation == UIInterfaceOrientationPortraitUpsideDown){
return AVCaptureVideoOrientationPortraitUpsideDown;
} else {
@throw @"unknown interface orientation";
}
}

static void * CapturingStillImageContext = &CapturingStillImageContext;
static void * SessionRunningContext = &SessionRunningContext;

Expand Down Expand Up @@ -127,6 +141,7 @@ @implementation CKCamera
- (void)dealloc
{
[self removeObservers];
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
}

-(PHFetchOptions *)fetchOptions {
Expand Down Expand Up @@ -183,18 +198,16 @@ - (instancetype)initWithFrame:(CGRect)frame {

[self handleCameraPermission];

#if !(TARGET_IPHONE_SIMULATOR)
[self setupCaptureSession];
#endif
self.previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.session];
[self.layer addSublayer:self.previewLayer];
self.previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;

#if (TARGET_IPHONE_SIMULATOR)
// Create mock camera layer. When a photo is taken, we capture this layer and save it in place of a
// hardware input.
self.mockPreview = [[CKMockPreview alloc] initWithFrame:CGRectZero];
[self addSubview:self.mockPreview];
#else
self.previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.session];
[self.layer addSublayer:self.previewLayer];
self.previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
[self setupCaptureSession];
#endif

UIView *focusView = [[UIView alloc] initWithFrame:CGRectZero];
Expand Down Expand Up @@ -391,10 +404,17 @@ - (void) setupCaptureSession {
}

[self.session commitConfiguration];

dispatch_async(dispatch_get_main_queue(), ^{
[self setInitialPreviewLayerVideoOrientation];
});
} );
}

- (void)setInitialPreviewLayerVideoOrientation{
UIInterfaceOrientation initialInterfaceOrientation = [UIApplication sharedApplication].statusBarOrientation;
self.previewLayer.connection.videoOrientation = AVCaptureVideoOrientationFromInterfaceOrientation(initialInterfaceOrientation);
}

-(void)handleCameraPermission {

switch ( [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo] )
Expand Down Expand Up @@ -934,6 +954,10 @@ - (void)willEnterForeground:(NSNotification *)notification {

#pragma mark - observers

- (void)didChangeStatusBarOrientation:(NSNotification *)notification {
UIInterfaceOrientation currentInterfaceOrientation = [UIApplication sharedApplication].statusBarOrientation;
self.previewLayer.connection.videoOrientation = AVCaptureVideoOrientationFromInterfaceOrientation(currentInterfaceOrientation);
}

- (void)addObservers
{
Expand All @@ -958,7 +982,10 @@ - (void)addObservers
selector:@selector(willEnterForeground:)
name:UIApplicationWillEnterForegroundNotification
object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(didChangeStatusBarOrientation:)
name:UIApplicationDidChangeStatusBarOrientationNotification
object:nil];
self.isAddedOberver = YES;
}
}
Expand Down

0 comments on commit 0fb5003

Please sign in to comment.