Veera Manikandan
Veera Manikandan

Reputation: 1

How to flip inverted selfie image in UIImagePickerController in preview view in swift 5

Initializing image picker

       imagePicker.sourceType = UIImagePickerController.SourceType.camera
            //If you dont want to edit the photo then you can set allowsEditing to false
            imagePicker.allowsEditing = false
            imagePicker.delegate = self
            imagePicker.sourceType = .camera
            imagePicker.cameraDevice = .front

            imagePicker.cameraCaptureMode = .photo
//            imagePicker.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext
            imagePicker.cameraOverlayView = nil
            imagePicker.showsCameraControls = true
           
            

            self.present(imagePicker, animated: true, completion: nil)

extension of ImagePickerController

 func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        guard var image = info [.originalImage] as? UIImage else {
                    return
                }
       
        if picker.cameraDevice == UIImagePickerController.CameraDevice.front {
            image = UIImage(cgImage: image.cgImage!, scale: image.scale, orientation:.leftMirrored)
        } else {
            print("back")

        }

        picker.dismiss(animated: true)

        }

The image taken from the selfie camera gets flipped. How can we fix this?

Upvotes: 0

Views: 414

Answers (1)

Ahmet Bostancikli
Ahmet Bostancikli

Reputation: 43

 //MARK: -  CGAffineTransform(scaleX: CGFloat, y: CGFloat) initializer helps flipping image according to x and y values
        image.transform = CGAffineTransform(scaleX: -1, y: 1)
//image is imageView

Helpful references:  You can find the all project codes in GitHub repo link and the other methods of flipping, and descriptions are exist in apple documentation link from below links. 
https://github.com/ahmetbostanciklioglu/ImagePickerController.git
https://developer.apple.com/documentation/coregraphics/1455016-cgaffinetransformmakescale

Upvotes: -1

Related Questions