Front facing camera in UIImagePickerController

后端 未结 9 1247
暗喜
暗喜 2020-12-02 06:42

I am developing the front facing camera app in iPad2 by using the UIImagePickerController.

When I capture the image it\'s shows as flipped from left to

9条回答
  •  一个人的身影
    2020-12-02 07:22

    It took me few hours, but I think I got there. Here is a working solution for Swift 5.2 of how to get correct image (both in ImagePicker preview and in output).

    
         //Registering to get notification when users takes a picture
    
        override func viewDidLoad() {
            NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue: "_UIImagePickerControllerUserDidCaptureItem"), object: nil, queue: nil) { (notification) in
                self.changePhotoOrientation()
        }
    
        //Changing image orientation for ImagePicker preview
    
        func changePhotoOrientation() {
            var subviews: [UIView] = [imagePicker.view]
            while (!subviews.isEmpty) {
                let subview = subviews.removeFirst()
                subviews += subview.subviews
                if (subview.isKind(of: UIImageView.self)) {
                    subview.transform = self.imagePicker.cameraViewTransform.scaledBy(x: -1, y: 1)
                }
            }
        }
    
        //Changing image orientation for the output image
    
        func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
            if let userPickedImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
                image = UIImage(cgImage: userPickedImage.cgImage!, scale: userPickedImage.scale, orientation: .leftMirrored)
            }
        }
    
    }
    
    

提交回复
热议问题