Search code examples
iosiphoneswiftcameraswift3

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!


Solution

  • 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