Swift UI camera shows portrait view but image turns out landscape
It returns only on landscape because the image which was taken with in-app camera has an orientation. You need to normalize it and display it.
I make an extension of UIImage with
func fixOrientation(img:UIImage) -> UIImage {
if (img.imageOrientation == UIImageOrientation.Up) {
return img;
}
UIGraphicsBeginImageContextWithOptions(img.size, false, img.scale);
let rect = CGRect(x: 0, y: 0, width: img.size.width, height: img.size.height)
img.drawInRect(rect)
let normalizedImage : UIImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext();
return normalizedImage;
}
Original answer from here:
Normalize Pictures taken by camera
Also I just did a few changes to copy and paste solution for the ImagePickerManager:
ImagePickerManager
Camera just works in Portrait mode
You should set your AVCaptureSession
orientation every time device's orientation is changed.
Add a Notification observer to observe any change in device orientation then set AVCaptureSession
orientation according to device orientation.
Obj-C:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(orientationChanged)
name:UIDeviceOrientationDidChangeNotification
object:nil];
-(void) orientationChanged
{
if (self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
[_previewLayer.connection setVideoOrientation:AVCaptureVideoOrientationPortraitUpsideDown];
if (self.interfaceOrientation == UIInterfaceOrientationPortrait)
[_previewLayer.connection setVideoOrientation:AVCaptureVideoOrientationPortrait];
if (self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft)
[_previewLayer.connection setVideoOrientation:AVCaptureVideoOrientationLandscapeLeft];
if (self.interfaceOrientation == UIInterfaceOrientationLandscapeRight)
[_previewLayer.connection setVideoOrientation:AVCaptureVideoOrientationLandscapeRight];
}
Swift 2:
Add observer somewhere, for example in viewDidLoad
method.
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(orientationChanged), name: UIDeviceOrientationDidChangeNotification, object: nil)
Then:
func orientationChanged(){
let deviceOrientation = UIDevice.currentDevice().orientation
if deviceOrientation == UIDeviceOrientation.PortraitUpsideDown {
previewLayer.connection.videoOrientation = AVCaptureVideoOrientation.PortraitUpsideDown
}
else if deviceOrientation == UIDeviceOrientation.Portrait {
previewLayer.connection.videoOrientation = AVCaptureVideoOrientation.Portrait
}
else if deviceOrientation == UIDeviceOrientation.LandscapeLeft {
previewLayer.connection.videoOrientation = AVCaptureVideoOrientation.LandscapeLeft
}
else if deviceOrientation == UIDeviceOrientation.LandscapeRight {
previewLayer.connection.videoOrientation = AVCaptureVideoOrientation.LandscapeRight
}
}
You can also provide any of your code or look at this question which has been asked before it may help.
AVCaptureSession with multiple orientations issue
Related Topics
Xcode 8.3 Swift Version Error (Swift_Version) in Objective C Project
Uitableview Has Unwanted Animation When Reloaddata Is Called
Populating Input Text Field in Wkwebview
Adding an Skscene to a Uiviewcontroller
Uibutton Target Action Inside Uiview
Is There an iOS API for Accessing Call Logs
Can't Add a New iOS Version for My App
Invalid Update: Invalid Number of Sections in Uitableview
Load Custom Error HTMLstring When Wkwebview Loadrequest Fails
Swift + Firebase Security Not Working
Using String.Characterview.Index.Successor() in for Statements
Core Data - Fetch All Entities Using the Same Field
Notification in Swift Every Day at a Set Time
Uiviewrepresentable Automatic Size - Passing UIkit UIview Size to Swiftui