IOS Swift 3: Flip Front Camera Image Horizontally after taking Camera Picture

I've ran into this problem before. I honestly don't understand it completely, but this is what my problem was and it probably has something to do with your problem:

What is happening is the iPhone cameras (likely due to some hardware reason) don't always save the image data in the correct orientation. Depending one the iPhone model and which camera it is, they can be saved upside down, sideways, and/or even flipped on one or both of the axis. Whenever you take a picture on the iPhone, it sometimes to "correct" this by applying EXIF transformation metadata to reverse it.

In the case of transformations, the EXIF metadata tells how to rotate and flip the data after its been opened. My guess why this exists is because it's cheaper to apply EXIF transformation metadata to an image rather than transforming the actual image data in memory.

This is sometimes problematic because not everything that displays an image in iOS respects the transformation metadata. This might be happening with the WKWebview. It could be displaying the image without checking the EXIF data for transformations.

I ran into this problem when building a couple of image-processing apps in the past. While I don't have any easy fixes for you, here's how I solved it: 1) First strip all the EXIF transformation data from your UIImage. This is more computationally expensive, but IMO it's worth it because it makes the images much easier to handle and you don't have to worry about things not handling EXIF data. 2) Then properly transform the image in the correct way if the iPhone camera saved it in an undesirable orientation.

These might be a good place to start

Stripping EXIF:

https://stackoverflow.com/a/34161629/4102858,

https://stackoverflow.com/a/25595495/4102858

UIImage transformations:

https://stackoverflow.com/a/29753437/4102858

These might not be perfect fixes for you, but hopefully they will help you understand your problem a little better.


Try This:

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)
  }
}

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