Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

There are a couple of questions on StackOverflow that deal with image flipping such as this one here. By default, iOS inverts the front camera horizontal image when a picture is taken. I am trying to prevent the front camera image only from being flipped or to flip it back to its proper orientation. I am interacting with a WKWebview.

The problem is that I don't know what method to call or put in my ViewController to get camera and then set it to the proper orientation, or the correct setting to prevent this behavior. I also don't know how to get the camera information that took the image.

Here is one solution I attempted based on translating some Objective-C code to change the image after the camera was done with the photo. However the picture variable is a constant and can't be changed:

func didTakePicture(_ picture: UIImage) {
    var flippedImage = UIImage(cgImage: picture.cgImage!, scale: picture.scale, orientation: .leftMirrored)
    picture = flippedImage
}

Any help would be greatly appreciated. Thank you!

like image 829
applecrusher Avatar asked Nov 17 '16 21:11

applecrusher


2 Answers

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

like image 147
Rajat Avatar answered Oct 11 '22 05:10

Rajat


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.

like image 29
WaltersGE1 Avatar answered Oct 11 '22 07:10

WaltersGE1