Search code examples
iosobjective-cuiimagepickercontroller

UIImagePickerController front camera show mirror image on preview view


I know many people had asked this question, but I am not found some correct answer to fix this issue,My problem is : How can I make the photo on preview view(after user tap the capture button,and then go to the preview view to let the user preview) not mirror, can this prossible fix in UIImagePickerController, I know how to rotate the photo , something in enum in UIImageOrientation like UIImageOrientationUp , I know what this mean.

Try 1:

I attend to get the photo after user tap capture button before go to the preview view and use the photo which I have mirror to cover the default photo (use cameraOverlayView), I can detect the user tap the capture button use NSNotificationCenter with @"_UIImagePickerControllerUserDidCaptureItem", but I find no way to get the photo.

Try 2:

I use cameraViewTransform, but when user take photo (front camera) when the Home button on the bottom or top, the image on preview view is not mirror, but when the Home button on the left or right, the image on phone have some problem before user tap the capture, although the image on the preview view is not mirror.use AVCaptureDeviceDidStartRunningNotification notification.

- (void)cameraChanged:(NSNotification *)notification
{
  if(self.imagePickerController.cameraDevice == UIImagePickerControllerCameraDeviceFront)
  {
     self.imagePickerController.cameraViewTransform = CGAffineTransformIdentity;
     self.imagePickerController.cameraViewTransform = CGAffineTransformScale(self.imagePickerController.cameraViewTransform, -1, 1);
     if (self.imagePickerController.cameraOverlayView) {
        NSLog_DEBUG(@"self.imagePickerController.cameraOverlayView is not nil");
    } else {
        NSLog_DEBUG(@"self.imagePickerController.cameraOverlayView is nil");
    }
    self.imagePickerController.cameraOverlayView.transform = CGAffineTransformScale(self.imagePickerController.cameraViewTransform, -1, 1);
  } else {
    self.imagePickerController.cameraViewTransform = CGAffineTransformIdentity;
  }
}

Solution

  • finally, i can not solve this issue in UIImagePickerController, but i solve this issue using AVFoundation and CoreMotion, you can see AVCam

    //init the motionManager
    - (void)initializeMotionManager{
      motionManager = [[CMMotionManager alloc] init];
      motionManager.accelerometerUpdateInterval = .1;
      motionManager.gyroUpdateInterval = .1;
    
      [motionManager startAccelerometerUpdatesToQueue:[NSOperationQueue currentQueue]
                                        withHandler:^(CMAccelerometerData  *accelerometerData, NSError *error) {
                                            if (!error) {
                                                [self outputAccelertionData:accelerometerData.acceleration];
                                            }
                                            else{
                                                NSLog_DEBUG(@"%@", error);
                                            }
                                        }];
      }
    
    //detect the device orientation
    - (void)outputAccelertionData:(CMAcceleration)acceleration{
    
      if (acceleration.x >= 0.75) {
          _orientationNew = UIInterfaceOrientationLandscapeLeft;
      }
      else if (acceleration.x <= -0.75) {
          _orientationNew = UIInterfaceOrientationLandscapeRight;
      }
      else if (acceleration.y <= -0.75) {
          _orientationNew = UIInterfaceOrientationPortrait;
      }
      else if (acceleration.y >= 0.75) {
          _orientationNew = UIInterfaceOrientationPortraitUpsideDown;
      }
      else {
          // Consider same as last time
          return;
      }
    
      if (_orientationNew == _orientationLast)
          return;
    
      _orientationLast = _orientationNew;
    }