Front facing camera in UIImagePickerController

后端 未结 9 1232
暗喜
暗喜 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:46

    Full Working Example in Swift, which answers to the initial question of this post (tested on an iPhone 5c using iOS 8.2):

            import UIKit
    
            class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UIActionSheetDelegate {
    
             @IBOutlet var myUIImageView: UIImageView!
    
             var myUIImagePickerController: UIImagePickerController!
    
             override func viewDidLoad() {
                 super.viewDidLoad()
             }
    
             override func viewWillAppear(animated: Bool) {
                 println("viewWillAppear(animated: Bool) method called.")
                 super.viewWillAppear(animated)
                 NSNotificationCenter.defaultCenter().removeObserver(self)
             }
    
             override func viewWillDisappear(animated: Bool) {
                 println("viewWillDisappear(animated: Bool) method called.")
                 super.viewWillDisappear(animated)
                 NSNotificationCenter.defaultCenter().addObserver(self, selector: "cameraChanged:", name: "AVCaptureDeviceDidStartRunningNotification", object: nil)
             }
    
             /* UIImagePickerControllerDelegate Section */
             func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
                  if(self.myUIImagePickerController.sourceType == UIImagePickerControllerSourceType.Camera) {
                     self.myUIImageView.image = info[UIImagePickerControllerEditedImage] as? UIImage
                  } else {
                     self.myUIImageView.image = info[UIImagePickerControllerOriginalImage] as? UIImage
                  }
                  self.dismissViewControllerAnimated(true, completion: nil)
             }
    
            func imagePickerControllerDidCancel(picker: UIImagePickerController) {
                self.dismissViewControllerAnimated(true, completion: nil)
            }
    
            /*
            You can choose to use one of the UIResponder methods:
            touchesBegan, touchesMoved, touchesEnded etc, in order to detect the touch
            on the UIImageView.
            */
            override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
                let touch: UITouch? = touches.anyObject() as? UITouch
                if (touch?.view == myUIImageView) {
                    println("myUIImageView has been tapped by the user.")
                    self.takingAPictureUsingTheCamera()
                }
            }
    
            func takingAPictureUsingTheCamera() {
                self.myUIImagePickerController = UIImagePickerController()
                self.myUIImagePickerController.delegate = self // Set the delegate
                self.myUIImagePickerController.sourceType = UIImagePickerControllerSourceType.Camera
                self.myUIImagePickerController.cameraDevice = UIImagePickerControllerCameraDevice.Front
        //        self.myUIImagePickerController.editing = true
                self.myUIImagePickerController.allowsEditing = true
                self.presentViewController(self.myUIImagePickerController, animated: true, completion: nil)
            }
    
            func cameraChanged(notification: NSNotification) {
                 println("cameraChanged(notification: NSNotification) method called.")
                 self.myUIImagePickerController.cameraViewTransform = CGAffineTransformIdentity
                 if(self.myUIImagePickerController.cameraDevice == UIImagePickerControllerCameraDevice.Front){
                     self.myUIImagePickerController.cameraViewTransform = CGAffineTransformScale(self.myUIImagePickerController.cameraViewTransform, -1, 1)
                 }
            }
           }// End class
    

提交回复
热议问题