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

fslejnso  于 2023-01-01  发布在  Swift
关注(0)|答案(1)|浏览(179)

正在初始化图像选取器

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)

图像拾取器控制器的扩展

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)

        }

自拍相机拍摄的图像被翻转了。我们该如何解决这个问题?

owfi6suc

owfi6suc1#

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

相关问题