简体   繁体   English

如何在 swift 中的预览视图中翻转 UIImagePickerController 中的倒置自拍图像 5

[英]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 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?我们如何解决这个问题?

 //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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM