How to Fix capturing images with front facing camera mirrors image, AVFoundation,Swift
i figured this out myself, Here is the solution:
if captureDevice.position == AVCaptureDevicePosition.back {
if let image = context.createCGImage(ciImage, from: imageRect) {
return UIImage(cgImage: image, scale: UIScreen.main.scale, orientation: .right)
}
}
if captureDevice.position == AVCaptureDevicePosition.front {
if let image = context.createCGImage(ciImage, from: imageRect) {
return UIImage(cgImage: image, scale: UIScreen.main.scale, orientation: .leftMirrored)
}
}
Fix orientation of images taken with front camera ios
It turned out that i was changing the videoOrientation
when rotation did change, which was wrong. I had to move that logic before capturing the photo. Now it works fine. Also I've fixed the mirroring problem, by setting isVideoMirrored
to true if using the front camera.
LandscapeRight front camera photos capturing upside down
Start the configuration of the camera ...
if captureSession.canAddOutput(videoOutput) {
captureSession.addOutput(videoOutput)
let connection = videoOutput.connectionWithMediaType(AVMediaTypeVideo)
if connection.supportsVideoOrientation {
connection.videoOrientation = isFrontCamera ? AVCaptureVideoOrientation.LandscapeLeft : AVCaptureVideoOrientation.LandscapeRight
}
print("adding output")
} else {
print("Could not add front video output")
return
}
captureSession.commitConfiguration()
captureSession.startRunning()
... set up the preview layer for the camera.
func setupPreviewLayer() {
var previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
var bounds = cameraView.bounds
previewLayer.backgroundColor = UIColor.blackColor().CGColor
previewLayer.bounds = bounds
previewLayer.videoGravity = AVLayerVideoGravityResize
previewLayer.position = CGPointMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds))
previewLayer.connection.videoOrientation = recordingOrientation;
self.cameraView.layer.addSublayer(previewLayer)
}
You could try a few work arounds one option being:
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
if UIDevice.currentDevice().orientation.isLandscape.boolValue {
print("Landscape")
} else {
print("Portrait")
}
}
Another work around would be to rotate and/or flip the image after the picture is taken.
IOS Swift 3: Flip Front Camera Image Horizontally after taking Camera Picture
You are flipping your image correctly but why are you assigning back your flipped image to picture
variable you can use flippedImage
variable and pass it where it is required.
func didTakePicture(_ picture: UIImage) {
var flippedImage = UIImage(CGImage: picture.CGImage!, scale: picture.scale, orientation: .leftMirrored)
// Here you have got flipped image you can pass it wherever you are using image
}
What I am trying to do is prevent the front camera image only from
being flipped
If you are using Default Camera then it is not possible to prevent the camera to prevent flipping the image. To do so either you need to create your own camera using AVFoundation and need to apply your logics there.
If you are ready to use any thirds party library then you can check LEMirroredImagePicker
Related Topics
How to Detect That the App Is Running on a Jailbroken Device
What Does Get-Task-Allow Do in Xcode
Email & Phone Validation in Swift
Development Team Not Showing in Xcode
Presenting Viewcontroller with Navigationviewcontroller Swift
Arkit Hide Objects Behind Walls
Uicollectionview Insert Cells Above Maintaining Position (Like Messages.App)
Coremotion Updates in Background State
iOS 9 "Fbauth2" Missing from Info.Plist
How to Use Speech Recognition Inside the iOS Sdk
Front Facing Camera in Uiimagepickercontroller
How to Draw a Transparent Stroke (Or Anyway Clear Part of an Image) on the Iphone
iOS 11 Safe Area Layout Guide Backwards Compatibility
How to Draw a Circle in iOS Swift
Animating/Moving Views Under Usage of Autolayout