fixed so camera works in all orientations for ios

This commit is contained in:
Marcus Andersson 2017-12-20 11:42:35 +01:00
parent da1f0f2fdd
commit ec4ac6c74b
3 changed files with 20 additions and 12 deletions

View File

@ -13,10 +13,10 @@ const styles = StyleSheet.create({
flex: 1,
},
preview: {
flex: 1,
alignSelf: "stretch",
justifyContent: 'flex-end',
alignItems: 'center',
height: 200
alignItems: 'center'
},
overlay: {
position: 'absolute',
@ -187,6 +187,7 @@ export default class Example extends React.Component {
onZoomChanged={() => {}}
defaultTouchToFocus
mirrorImage={false}
cropToPreview={false}
/>
<View style={[styles.overlay, styles.topOverlay]}>
<TouchableOpacity

View File

@ -52,8 +52,8 @@
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationLandscapeRight</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>

View File

@ -627,15 +627,6 @@ RCT_EXPORT_METHOD(hasFlash:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRej
// create cgimage
CGImageRef cgImage = CGImageSourceCreateImageAtIndex(source, 0, NULL);
// Crop it (if capture quality is set to preview)
if (self.cropToPreview) {
CGSize viewportSize = CGSizeMake(self.previewLayer.frame.size.width, self.previewLayer.frame.size.height);
CGRect captureRect = CGRectMake(0, 0, CGImageGetWidth(cgImage), CGImageGetHeight(cgImage));
CGRect croppedSize = AVMakeRectWithAspectRatioInsideRect(viewportSize, captureRect);
cgImage = CGImageCreateWithImageInRect(cgImage, croppedSize);
}
// Rotate it
CGImageRef rotatedCGImage;
@ -665,6 +656,22 @@ RCT_EXPORT_METHOD(hasFlash:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRej
} else {
rotatedCGImage = cgImage;
}
// Crop it
if (self.cropToPreview) {
CGSize viewportSize;
if (UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation]))
{
viewportSize = CGSizeMake(self.previewLayer.frame.size.height, self.previewLayer.frame.size.width);
} else {
viewportSize = CGSizeMake(self.previewLayer.frame.size.width, self.previewLayer.frame.size.height);
}
CGRect captureRect = CGRectMake(0, 0, CGImageGetWidth(rotatedCGImage), CGImageGetHeight(rotatedCGImage));
CGRect croppedSize = AVMakeRectWithAspectRatioInsideRect(viewportSize, captureRect);
rotatedCGImage = CGImageCreateWithImageInRect(rotatedCGImage, croppedSize);
}
// Erase stupid TIFF stuff
[imageMetadata removeObjectForKey:(NSString *)kCGImagePropertyTIFFDictionary];