Skip to content
This repository was archived by the owner on Jun 16, 2023. It is now read-only.

Commit afa7bb1

Browse files
fix: fast capture orientation. (#3269)
Make sure orientation value stays consistent from the beginning to the end of the capture process. Co-authored-by: Cristiano Coelho <cristianocca@hotmail.com>
1 parent df0418e commit afa7bb1

File tree

1 file changed

+30
-23
lines changed

1 file changed

+30
-23
lines changed

ios/RN/RNCamera.m

+30-23
Original file line numberDiff line numberDiff line change
@@ -750,13 +750,24 @@ - (void)takePicture:(NSDictionary *)options resolve:(RCTPromiseResolveBlock)reso
750750
return;
751751
}
752752

753-
if (!self.deviceOrientation) {
754-
[self takePictureWithOrientation:options resolve:resolve reject:reject];
755-
return;
756-
}
757-
758-
NSInteger orientation = [options[@"orientation"] integerValue];
753+
// make sure to get orientation info here
754+
// as it may change if multiple consecutive calls are done
755+
NSInteger orientation;
756+
NSNumber* deviceOrientation;
757+
758+
@synchronized (self) {
759+
if (!self.deviceOrientation) {
760+
[self takePictureWithOrientation:options resolve:resolve reject:reject];
761+
return;
762+
}
759763

764+
orientation = [options[@"orientation"] integerValue];
765+
deviceOrientation = self.deviceOrientation;
766+
767+
self.orientation = nil;
768+
self.deviceOrientation = nil;
769+
}
770+
760771
AVCaptureConnection *connection = [self.stillImageOutput connectionWithMediaType:AVMediaTypeVideo];
761772
[connection setVideoOrientation:orientation];
762773
@try {
@@ -923,7 +934,7 @@ - (void)takePicture:(NSDictionary *)options resolve:(RCTPromiseResolveBlock)reso
923934
}
924935

925936
}
926-
937+
927938
CFDictionaryRef finalMetaData = nil;
928939
if (writeExif) {
929940
finalMetaData = (__bridge CFDictionaryRef)metadata;
@@ -950,7 +961,7 @@ - (void)takePicture:(NSDictionary *)options resolve:(RCTPromiseResolveBlock)reso
950961
}
951962

952963
bool success = YES;
953-
964+
954965
if (![options[@"doNotSave"] boolValue]) {
955966
NSString* pathRes = [RNImageUtils writeImage:destData toPath:path];
956967
if (!pathRes) {
@@ -959,9 +970,9 @@ - (void)takePicture:(NSDictionary *)options resolve:(RCTPromiseResolveBlock)reso
959970
} else {
960971
response[@"uri"] = pathRes;
961972
}
962-
973+
963974
}
964-
975+
965976
if (success) {
966977
response[@"width"] = @(takenImage.size.width);
967978
response[@"height"] = @(takenImage.size.height);
@@ -977,18 +988,15 @@ - (void)takePicture:(NSDictionary *)options resolve:(RCTPromiseResolveBlock)reso
977988
//[RNImageUtils updatePhotoMetadata:imageSampleBuffer withAdditionalData:@{ @"Orientation": @(imageRotation) } inResponse:response]; // TODO
978989
}
979990

980-
response[@"pictureOrientation"] = @([self.orientation integerValue]);
981-
response[@"deviceOrientation"] = @([self.deviceOrientation integerValue]);
982-
991+
response[@"pictureOrientation"] = @(orientation);
992+
response[@"deviceOrientation"] = @([deviceOrientation integerValue]);
993+
983994
if (useFastMode) {
984995
[self onPictureSaved:@{@"data": response, @"id": options[@"id"]}];
985996
} else {
986997
resolve(response);
987998
}
988999
}
989-
990-
self.orientation = nil;
991-
self.deviceOrientation = nil;
9921000
}
9931001
else{
9941002
reject(@"E_IMAGE_CAPTURE_FAILED", @"Image could not be saved", error);
@@ -1016,7 +1024,7 @@ - (void)takePicture:(NSDictionary *)options resolve:(RCTPromiseResolveBlock)reso
10161024
}
10171025

10181026
- (void)recordWithOrientation:(NSDictionary *)options resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject{
1019-
1027+
10201028
UIInterfaceOrientation orientation = [self.sensorOrientationChecker getDeviceOrientation];
10211029
NSMutableDictionary *tmpOptions = [options mutableCopy];
10221030
if ([tmpOptions valueForKey:@"orientation"] == nil) {
@@ -1111,7 +1119,6 @@ - (void)record:(NSDictionary *)options resolve:(RCTPromiseResolveBlock)resolve r
11111119
// with this and the captureAudio prop
11121120
dispatch_async(self.sessionQueue, ^{
11131121
[self initializeAudioCaptureSessionInput];
1114-
11151122
// finally, make sure we got access to the capture device
11161123
// and turn the connection on.
11171124
if(self.audioCaptureDeviceInput != nil){
@@ -1186,17 +1193,17 @@ - (void)record:(NSDictionary *)options resolve:(RCTPromiseResolveBlock)resolve r
11861193
if (options[@"codec"]) {
11871194
if (@available(iOS 10, *)) {
11881195
AVVideoCodecType videoCodecType = options[@"codec"];
1189-
1196+
11901197
if ([self.movieFileOutput.availableVideoCodecTypes containsObject:videoCodecType]) {
11911198
self.videoCodecType = videoCodecType;
1192-
1199+
11931200
BOOL supportsBitRate = NO;
1194-
1201+
11951202
// prevent crashing due to unsupported keys
11961203
if (@available(iOS 12.0, *)) {
11971204
supportsBitRate = [[self.movieFileOutput supportedOutputSettingsKeysForConnection:connection] containsObject:AVVideoCompressionPropertiesKey];
11981205
}
1199-
1206+
12001207
if(options[@"videoBitrate"] && supportsBitRate) {
12011208
NSString *videoBitrate = options[@"videoBitrate"];
12021209
[self.movieFileOutput setOutputSettings:@{
@@ -2017,7 +2024,7 @@ - (void)mirrorVideo:(NSURL *)inputURL completion:(void (^)(NSURL* outputUR))comp
20172024

20182025
[instruction setLayerInstructions:@[transformer]];
20192026
[videoComposition setInstructions:@[instruction]];
2020-
2027+
20212028
//get preset for export via default or session
20222029
AVCaptureSessionPreset preset = [self getDefaultPreset];
20232030
if (self.session.sessionPreset != preset) {

0 commit comments

Comments
 (0)